3 回答

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
為什么要使用自定義分隔符?您可以簡(jiǎn)單地接受由新行分隔的輸入。
Scanner input=new Scanner(System.in);
System.out.println("Press enter key after every input");
System.out.print("Enter the name of product 1, product 2, and product 3: ");
String prod1 = input.nextLine();
String prod2 = input.nextLine();
String prod3 = input.nextLine();
System.out.print("Enter the quantity of product 1, product 2, and product 3: ");
int quan1 = input.nextInt();
input.nextLine();
int quan2 = input.nextInt();
input.nextLine();
int quan3 = input.nextInt();
input.nextLine();
System.out.println(prod1+" "+prod2+" "+prod3);
System.out.println(quan1+" "+quan2+" "+quan3);

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
此異常由 Scanner 類的實(shí)例引發(fā),以指示檢索到的令牌與預(yù)期類型的模式不匹配,或者檢索到的令牌超出范圍

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
嘗試以下輸入: fish##banana##ice cream##3##4##5##
我猜想換行符也可能被讀取,因此第一個(gè) nextInt 失敗。也許您還想input.nextLine()
在##處使用和分割字符串?
添加回答
舉報(bào)