java - 在线程中获取异常,指向与我的类中的 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很陌生。 重新链接
解决方案
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
}
推荐阅读
- xml - 在scala中将XML元素解析为Dataframe
- javascript - 无法按时间间隔更新计数器
- android - 重新打开时是否可以在浏览器中重新加载网站
- spring-security - Spring Security 记住我复选框
- sql - 将日期行传输到时间戳行,将旧日期值与时间字符串 SQL 连接起来
- ruby-on-rails - 如何在开发中关闭 PumaWorkerKiller
- javascript - 为什么 HTMLElement 原型与所有实例共享属性而不是为每个元素创建副本?
- pdf - 为什么带有对象流的加密 PDF 对 Acrobat Reader 无效,但在任何其他查看器中都可以正常打开
- r - 如何合并ggplot2中的图例?(保持形状、颜色和标签)
- fonts - 哪种字体可以显示⯨(左半黑星)