Tag: inputmismatchexception

无法弄清楚如何捕获InputMismatchException

所以这是我当前捕获InputMismatchException错误的代码 int weapon = 0 boolean selection = true; while(selection) { try { System.out.println(“Pick number 1, 2, or 3.”); weapon = scan.nextInt(); selection = false; } catch(InputMismatchException e) { System.out.println(“Choose 1,2,3”); weapon = scan.nextInt(); } } 我正在尝试确保输入int而不是其他任何东西。 扫描仪类已经实现,“扫描”将对我起作用。 感谢您的帮助!

“线程中的exception”main“java.util.InputMismatchException”**

我已经搜索了但我似乎无法在代码中找到任何错误,请帮忙! 代码编译但是,当我想回答问题3时,这是我得到的错误: Exception in thread “main” java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at ForgetfulMachine.main(ForgetfulMachine.java:16) 这是我的代码: import java.util.Scanner; public class ForgetfulMachine { public static void main( String[] args ) { Scanner keyboard = new Scanner(System.in); System.out.println( “What city is the capital of Germany?” ); keyboard.next(); System.out.println( “What is 6 divided by 2?” […]

从文件读取双精度时输入不匹配

我必须编写一个程序,它将从文本文件“balances.txt”中读取名称和余额,并组织成一个报告,然后将余额总计为总计。 这是文件包含的内容: JAKIE JOHNSON,2051.59 SAMUEL PAUL SMITH,10842.23 ELISE ELLISON,720.54 我最初写的代码给了我我想要的东西,但被告知不要使用循环,数组或parseDouble 。 我现在尝试了以下操作,但每次使用nextDouble都会出现错误。 代码: import java.io.File; import java.text.NumberFormat; import java.text.DecimalFormat; import java.io.FileInputStream ; import java.io.FileNotFoundException ; import java.io.IOException ; import java.util.Scanner ; public class BankFile { public static void main(String[] args) throws IOException { Scanner fileIn = null; try { String filename = “balances.txt” ; File […]

捕获exception后for循环继续出现问题

嗨,我是java的半新人,不能想出这个。 捕获exception后,我想要一个for循环继续并继续读取新的整数。 这是一个在线挑战,希望你拿5(这说明它之后应该有多少输入), -150, 150000, 1500000000, 213333333333333333333333333333333333, -100000000000000. 并转入此输出: -150 can be fitted in: * short * int * long 150000 can be fitted in: * int * long 1500000000 can be fitted in: * int * long 213333333333333333333333333333333333 can’t be fitted anywhere. -100000000000000 can be fitted in: * long 我希望计算机检查一个数字对于byte,short,int和long是否不大。 它工作(可能不是最好的方式)直到它达到213333333333333333333333333333333333。它导致InputMismatchException(bc它变大)并且代码捕获它但是在它不起作用之后。 这是输出: -150 can […]

试图提示用户从catch块重新进入,但catch块终止了吗?

我正在尝试编写一个程序,要求用户输入他们的年龄并提示他们重新输入,如果他们输入了不正确的值(例如负数,年龄超过120,年龄有特殊字符或字母,范围号等…) 我尝试写一个try / catch来要求用户重新输入他们的年龄: System.out.println(“Enter your age (a positive integer): “); int num; try { num = in.nextInt(); while (num 120) { System.out.println(“Bad age. Re-enter your age (a positive integer): “); num = in.nextInt(); } } catch (InputMismatchException e) { //System.out.println(e); System.out.println(“Bad age. Re-enter your age (a positive integer): “); num = in.nextInt(); } 当输入的年龄包含特殊字符/字母或超出范围时,程序会打印出“Bad […]

线程“main”java.util.InputMismatchException中的exception

我需要帮助在java中进行一次练习,我可能会在2小时内遇到此错误。 任何帮助都会很棒。 Exception in thread “main” java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at prodavnica.Prodavnica.main(Prodavnica.java:60) Java Result: 1 package prodavnica; public class Proizvod { private String ime_proizvod; private static int cena; public Proizvod(String ime_proizvod, int cena) { this.ime_proizvod = ime_proizvod; this.cena=cena; } public String getIme_proizvod() { return ime_proizvod; } public void setIme_proizvod(String ime_proizvod) […]

为什么我得到InputMismatchException?

到目前为止我有这个: public double checkValueWithin(int min, int max) { double num; Scanner reader = new Scanner(System.in); num = reader.nextDouble(); while (num max) { System.out.print(“Invalid. Re-enter number: “); num = reader.nextDouble(); } return num; } 和这个: public void askForMarks() { double marks[] = new double[student]; int index = 0; Scanner reader = new Scanner(System.in); while (index < […]

扫描仪双值 – InputMismatchException

我尝试以最简单的方式使用扫描仪: 码: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in); System.out.print(“Enter the number of gallons of gas in the tank: “); gas = scanner.nextDouble(); System.out.print(“Enter the fuel efficiency: “); efficiency = scanner.nextDouble(); 但在第一次输入5.1它会抛出: Exception in thread “main” java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextDouble(Scanner.java:2456) at udacity.MileagePrinter.main(MileagePrinter.java:59) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) […]