Java,使用扫描器,异常 [英] Java, using scanner, exception

查看:134
本文介绍了Java,使用扫描器,异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人会告诉我我做错了什么,这部分代码应该扫描餐厅的名称,菜单名称和菜单项的名称及其价格,但它给我一些例外,也似乎跳过完全是第一次扫描,只是跳到第二个,理想情况下,它应该扫描1个餐厅,有1个菜单,包含2个菜单项,其名称和价格

  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(请输入新餐厅的名称:);
newRestaurant = scan.nextLine();
System.out.println(你想要创建的菜单的名称是什么(如果你已经完成,输入'none'):);
newMenu = scan.nextLine();
if(newMenu ==none)System.out.println(保存条目...);
else {
System.out.println(你想要创建的菜单项的名称是什么(如果你已经完成,输入'none'):);
newMenuItem [x] = scan.nextLine();
if(newMenu!=none){
System.out.println(什么是价格?);
price [x] = scan.nextDouble();
x ++;


解决方案

首先让我们从方式:


不要 == != 来测试Java中的字符串。除非你真的,真的知道你在做什么, == != 有责任给你错误的答案。使用 s.equals(s2)!s.equals(s2)


来比较字符串,这是测试不可靠的情况。修正所有这些...



参考:








现在我们有什么是导致 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:


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,使用扫描器,异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆