解决方案
首先让我们从方式:
不要 == 或 != 来测试Java中的字符串。除非你真的,真的知道你在做什么, == 和!= 有责任给你错误的答案。使用 s.equals(s2)或!s.equals(s2)。
来比较字符串,这是测试将不可靠的情况。修正所有这些...参考:
- 如何比较Java中的字符串?
现在我们有什么是导致 InputMismatchException 被抛出。你告诉我们,它来自一个简单的 println ,但这实际上是不可能的。实际上,stacktrace表示异常正在被一个 nextDouble()调用抛出,而且会发生,因为(因为 javadoc 说)...下一个标记不匹配浮点正则表达式,或超出范围。
是什么原因?这可能是两件事:
-
可能是用户输入的内容不是正确语法中的数字。例如,四或我的大脑伤害。
正确写入的程序应该通过告诉用户他/她犯了一个错误。 (例如,使用 hasDouble()方法来测试下一个令牌是否可以接受为 double 。)
-
可能是您的代码中的逻辑错误意味着您尝试将一些其他问题(或某事)的答案读为数字。 p>
我不能告诉你哪些是真正的原因,因为坦白说我不相信证据你提供的我怀疑那是真正的代码,它当然不匹配stacktrace。此外,您还没有告诉我们用户正在输入什么来导致这种情况。
Could someone show me what i'm doing wrong, this part of code should scan for name of restaurant, menu name and names of menu items and their prices, but it's giving me some sort of exception, also it seems to skip completely the first scan, just jumps to second, ideally it should scan for 1 resturant that has 1 menu and that contains 2 menu items with their names and prices
String newRestaurant, newMenu; String[] newMenuItem = { "", "" }; double[] price = {0.0, 0.0}; int x = 0; System.out.println("---------------------------------------"); System.out.println("CREATE A RESTAURANT, MENUS, and MENU ITEMS:"); System.out.println("Please input the name of the new Restaurant:"); newRestaurant = scan.nextLine(); System.out.println("What is the name of the Menu you wish to create (type 'none', if you are done):"); newMenu = scan.nextLine(); if (newMenu == "none") System.out.println("Saving entry..."); else { System.out.println("What is the name of the Menu item you wish to create (type 'none', if you are done):"); newMenuItem[x] = scan.nextLine(); if(newMenu != "none") { System.out.println("What is the price?"); price[x]= scan.nextDouble(); x++;解决方案
First lets get the problem of your string testing out of the way:
Don't == or != to test strings in Java. Unless you really, really know what you are doing, == and != are liable to give your the wrong answer. Use s.equals(s2) or !s.equals(s2).
Your original code uses both == and != to compare strings, and this is a situation where the test will be unreliable. Fix all of these ...
Reference:
- How do I compare strings in Java?
Now we have the problem of what is causing InputMismatchException to be thrown. You have told us that it is coming from a simple println, but that is virtually impossible. In reality, the stacktrace says that exception is being thrown by a nextDouble() call, and that will be happening because (as the javadoc says) "... the next token does not match the Float regular expression, or is out of range".
What is the cause? Well it could be two things:
It could be that the user has entered something that is not a number in the right syntax. For example, "four" or "my brain hurts".
A properly written program should deal with this ... by telling the user he/she has made a mistake. (For example, use the hasDouble() method to test if the next token is acceptable as a double.)
It could be that a logic error in your code means that you are attempting to read the answer to some other question (or something) as a number.
I can't tell you which of those is the real cause, because frankly I don't trust the "evidence" that you have provided. I doubt that that is the real code, and it certainly doesn't match the stacktrace. Furthermore, you haven't told us what the "user" is typing to cause this to happen.
更多推荐
Java,使用扫描器,异常
发布评论