首页 > 解决方案 > 在线程中获取异常,指向与我的类中的 double 和方法相关的行

问题描述

我正在编写一个简单的 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很陌生。 重新链接

标签: javaclassdoublejava.util.scanner

解决方案


Your else case executed somehere and in your calculatePerson method you parse stringinto double

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

that's why you'll get exception.

 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
}

推荐阅读