在线程中获取异常,指向与我的类中的double和方法相关的行

问题描述 投票:-1回答:2

我正在编写简单的java程序,模拟简单的餐馆注册/书籍系统。我得到了它的工作,有点,但有一个问题我似乎无法解决。一旦我运行程序,注册一个新的餐厅,然后预订,我收到此错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "None"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at Restaurant.calculatePercent(Restaurant.java:69)
    at Main.main(Main.java:254)

我的Restaurant.java:69是一种计算折扣百分比的方法,与用户选择的餐厅可享受折扣的时间范围相比较。

 public void calculatePercent(double bookTime){
        if (Double.parseDouble(startTime10) <= bookTime && bookTime <= Double.parseDouble(endTime10)){
            discountPercentage = 10;
        }
        else if (Double.parseDouble(startTime20) <= bookTime && bookTime <= Double.parseDouble(endTime20)){
            discountPercentage = 20;
        }
        else if (Double.parseDouble(startTime25) <= bookTime && bookTime <=
                Double.parseDouble(endTime25)){
            discountPercentage = 25;
        }
        else if (Double.parseDouble(startTime50) <= bookTime && bookTime <= Double.parseDouble(endTime50)){
            discountPercentage = 50;
        }
        else{
            discountPercentage = 0;
        }
    }

    public void calculatePercent(String bookTime){
        discountPercentage = 0;
    }

我的Main.java:254

restaurantList.get(restaurantIndex).calculatePercent(bookTime);

变量bookTime是double类型,我这样声明:


    System.out.print("Please put in the time you would like to book\n->");
    double bookTime = Double.parseDouble(input.nextLine());
    if (bookTime > 100){
        bookTime /= 100;
      }
      else{bookTime = bookTime;}

这是我在repl上的完整代码的链接。我为这个烂摊子道歉。我对java很新。 repl link

java class double java.util.scanner
2个回答
0
投票

你的其他情况在某处执行,在你的qazxsw poi方法中,你将字符串解析为double

calculatePerson

这就是为什么你会得到例外。

if (Double.parseDouble(startTime10) <= bookTime && bookTime <= Double.parseDouble(endTime10)){
      discountPercentage = 10;
    }

0
投票

正如你可以看到日志: -

if (ask10.toLowerCase().equals("y")){ //.... your code } else{ startTime10 = "None"; //<--------------------------- here endTime10 = "None"; //<--------------------------- here } //20% System.out.print("Does the restaurant has 20% discount time? Y/N: "); String ask20 = input.nextLine(); String startTime20, endTime20; if (ask20.toLowerCase().equals("y")){ //.... your code } else{ startTime20 = "None"; //<--------------------------- here endTime20 = "None"; //<--------------------------- here } //25% System.out.print("Does the restaurant has 25% discount time? Y/N: "); String ask25 = input.nextLine(); String startTime25, endTime25; if (ask25.toLowerCase().equals("y")){ //.... your code }else{ startTime25 = "None"; //<--------------------------- here endTime25 = "None"; //<--------------------------- here } //50% System.out.print("Does the restaurant has 50% discount time? Y/N: "); String ask50 = input.nextLine(); String startTime50, endTime50; if (ask50.toLowerCase().equals("y")){ //.... your code else{ startTime50 = "None"; //<--------------------------- here endTime50 = "None"; //<--------------------------- here }

在解析为double时,它期望有效数字而不是像Exception in thread "main" java.lang.NumberFormatException: For input string: "None" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)这样的字符串。

'None'

你的代码中的else块就像行parseDouble returns a primitive double containing the value of the string #98一样,当用户没有添加#99startTime字符串时,它们明确地将endTime'None'字符串设置为is10DiscountAvailable。当要将其解析为Double时,它会抛出异常。

解决方案:找到不同的方法。也许您可以根据用户的选择将qazxswpoi这样的旗帜设置为true或false,然后继续阅读并设置折扣的开始和结束时间。

添加:您的代码也可以使用异常处理,它在很多情况下都很有用。尝试学习并将其包含在代码中,因为此应用程序中的输入需要进行大量验证检查。

© www.soinside.com 2019 - 2024. All rights reserved.