线程“中出现异常时出错”;“主要”;java.lang.NumberFormatException:对于输入字符串:"&引用;

线程“中出现异常时出错”;“主要”;java.lang.NumberFormatException:对于输入字符串:"&引用;,java,Java,我正在做一个网上购物的学校项目,出现了一个错误,在我输入y后,有一个选择你想要购买的产品的部分,在变量选择中,cmd会自动输入“” System.out.println(">Mobiles,Computers"); System.out.println(">TV,Appliances,Electronics"); System.out.println(">Men's Fashion"); System.out.println(">Women's

我正在做一个网上购物的学校项目,出现了一个错误,在我输入y后,有一个选择你想要购买的产品的部分,在变量选择中,cmd会自动输入“”

 System.out.println(">Mobiles,Computers");
    System.out.println(">TV,Appliances,Electronics");
    System.out.println(">Men's Fashion");
    System.out.println(">Women's Fashion");
    System.out.println(">Home accessories");
    System.out.println("do you want to choose your product from above catagories or not \n enter y/n (REPLY IN SMALL LETTERS");
    c1=(char)br.read();
    while(c1 != 'n')
    {
    System.out.println("-----------------------------------------------------------------------");
    System.out.println("Enter your choice");
    System.out.println("Press 1: Mobiles,Laptops");
    System.out.println("Press 2: TV,Appliances,Electronics");
    System.out.println("Press 3: Men's Fashion");
    System.out.println("Press 4: Women's Fashion ");
    System.out.println("Press 5: Home accessories");
    System.out.println("-----------------------------------------------------------------------");
    int choice=Integer.parseInt(br.readLine()); 
    switch(choice)
    { 

如果该行为空,则可以跳过该行:

String line = br.readLine();
if (line.isEmpty()) continue;
int choice = Integer.parseInt(br.readLine()); 
在这里:

从输入中读取单个字符。我想你一定进去了

延特

在键盘上。事情是这样的:你只是在读一个字符,而不是换行符/换行符。因此,该部分“保留”在您的输入中,当您执行后续的
readLine()
时,您将得到该输入部分,最终变成一个空字符串

一种解决方案是:使用
readLine()
执行所有读取操作,然后只需执行以下操作即可

while(!stringFromUser.equalsIgnoreCase("n)) { 
比如说。或者,在执行
Integer.parseInt(br.readLine())之前,您可以简单地执行一个“伪”的
readLine()
调用


最后:我假设您在System.in上使用(缓冲)读取器。那没什么意义。您可以使用
扫描仪
。(请参阅以获取一些指导)

使用扫描仪从控制台读取:

Scanner in = new Scanner(System.in); 
String s = in.nextLine();

尝试使用扫描仪从控制台读取:
Scanner in=new Scanner(System.in);字符串s=in.nextLine()
Scanner in = new Scanner(System.in); 
String s = in.nextLine();