Java 第二个扫描仪对象的输入与类型不匹配。。?
我正在写一个小程序,其中用户输入两个日期,程序计算第一个和第二个日期之间的时间Java 第二个扫描仪对象的输入与类型不匹配。。?,java,Java,我正在写一个小程序,其中用户输入两个日期,程序计算第一个和第二个日期之间的时间 String amPm1, amPm2, temp; int ore1, ore2, minuti1, minuti2, secondi1, secondi2, ore, minuti, secondi; Scanner input1 = new Scanner(System.in); input1.useDelimiter(":"); Syst
String amPm1, amPm2, temp;
int ore1, ore2, minuti1, minuti2, secondi1, secondi2, ore, minuti, secondi;
Scanner input1 = new Scanner(System.in);
input1.useDelimiter(":");
System.out.println("Inserisci due orari separati e ti dirò la differenza di tempo chew intercorre tra i due.");
System.out.println("L'orario deve essere scritto secondo il seguente formato HH:MM:SS AM/PM");
System.out.println("Primo orario: ");
ore1 = input1.nextInt();
minuti1 = input1.nextInt();
temp = input1.nextLine();
secondi1 = Integer.parseInt(temp.substring(1, (temp.length() - 3)));
amPm1 = temp.substring((temp.length() - 2), temp.length());
System.out.printf("ore: %d\nminuti: %d\nsecondi: %d\n%s\n", ore1, minuti1, secondi1, amPm1);
if ((ore1 > 12 || ore1 < 1) || (minuti1 > 59 || minuti1 < 0) || (secondi1 > 59 || secondi1 < 0) ||
!(amPm1.equalsIgnoreCase("AM")) || (amPm1.equalsIgnoreCase("PM")))
System.out.println("Errore - l'orario inserito non è valido.");
System.out.println("Secondo orario: ");
Scanner input2 = new Scanner(System.in);
input2.useDelimiter(":");
ore2 = input2.nextInt();
System.out.println(ore2);
minuti2 = input2.nextInt();
System.out.println(minuti2);
temp = input2.nextLine();
System.out.println(temp);
secondi2 = Integer.parseInt(temp.substring(1, (temp.length() - 3)));
System.out.println(secondi2);
amPm2 = temp.substring((temp.length() - 2), temp.length());
System.out.println(amPm2);
字符串amPm1、amPm2、温度;
内部ore1、ore2、分钟1、分钟2、秒1、秒2、ore1、分钟2、秒1;
扫描仪输入1=新扫描仪(System.in);
input1.useDelimiter(“:”);
系统输出println(“因时间不同而产生的插入或分离。”);
系统输出打印LN(“L'orario devesessere scritto secondo il seguente formato HH:MM:SS AM/PM”);
System.out.println(“Primo-orario:”);
ore1=input1.nextInt();
minuti1=input1.nextInt();
temp=input1.nextLine();
secondi1=Integer.parseInt(临时子字符串(1,(临时长度()-3));
amPm1=温度子字符串((温度长度()-2),温度长度());
System.out.printf(“ore:%d\n分钟:%d\n秒:%d\n%s\n”,ore1,分钟1,秒1,amPm1);
如果(或1>12 | | |或1<1)| | | | | | | | | |分钟1<0)| | | | |
!(amPm1.equalsIgnoreCase(“AM”))| |(amPm1.equalsIgnoreCase(“PM”))
System.out.println(“Errore-l'orarioinserito nonèvalido.”);
System.out.println(“Secondo orario:”);
扫描仪输入2=新扫描仪(System.in);
input2.useDelimiter(“:”);
ore2=input2.nextInt();
系统输出打印项次(ore2);
minuti2=input2.nextInt();
系统输出打印项次(分钟2);
temp=input2.nextLine();
系统输出打印项次(温度);
secondi2=Integer.parseInt(临时子字符串(1,(临时长度()-3));
System.out.println(secondi2);
amPm2=温度子字符串((温度长度()-2),温度长度());
系统输出打印项次(amPm2);
当我输入第二个日期的数字时,第二个Scanner对象返回一个InputMismatchException:
线程“main”java.util.InputMismatchException中出现异常
就在这一行:
ore2=input2.nextInt() 您不应该混合使用nextLine和其他nextXYZ(例如nextInt)方法;这些方法之间的交互会变得混乱和复杂,并且容易引入错误。只需执行一个nextLine,并使用regex或其他方法解析出所需的位。还有,为什么要创建第二个扫描程序?我尝试使用Integer.parseInt(input1.nextLine())并删除了第二个扫描程序。结果它返回一个NumberFormatException错误。此外,如果我不使用解析,只需删除第二个扫描程序,程序允许我对第一个日期进行数字运算,然后打印带有“Secondo orario”的行,并返回InputMismatchException。如果您是第一个注释,您应该解析nextLine()中的字符串然后使用正则表达式提取所需的位。或者至少在尝试盲目地将格式传递给parseInt之前验证格式。