import java.util.ArrayList;
import javax.swing.JOptionPane;
public class MainApp {
public static void main(String[] args) {
ArrayList<Product> cart = new ArrayList<Product>();
String s1 = JOptionPane
.showInputDialog("Please enter the quantity of the product you have selected");
int r = Integer.parseInt(s1);
String s2 = JOptionPane
.showInputDialog("Please enter the name of the product");
String s3 = JOptionPane
.showInputDialog("Please enter the price of the product");
double d1 = Double.parseDouble(s3);
for (int i = 0; i < r; i++) {
Product p1 = new Product();
p1.setName(s2);
p1.setPrice(d1);
cart.add(p1);
}
double total_price = 0;
for (int i = 0; i < cart.size(); i++) {
total_price = total_price + cart.get(i).getPrice();
}
JOptionPane.showMessageDialog(null, cart.toString()
+ "The total price of all the products in the cart is : "
+ total_price + " KD", "Shopping Information",
JOptionPane.INFORMATION_MESSAGE);
cart.removeAll(cart);
JOptionPane.showMessageDialog(null, "Thank you for shopping with us! ",
"Prompt", JOptionPane.INFORMATION_MESSAGE);
String s4 = JOptionPane
.showInputDialog("Would you like to add items to your cart");
while (s4 != "stop") {
String s5 = JOptionPane
.showInputDialog("Please enter the name of the product");
String s6 = JOptionPane
.showInputDialog("Please enter the price of the product");
double d2 = Double.parseDouble(s6);
Product p2 = new Product();
p2.setName(s5);
p2.setPrice(d2);
cart.add(p2);
String s7 = JOptionPane
.showInputDialog("Would you like to add more items?");
if (s7 == "No" || s7 == "no")
s4 = "stop";
}
JOptionPane.showMessageDialog(null, cart.toString(), "Cart Details",
JOptionPane.INFORMATION_MESSAGE);
}
}
一切工作正常,直到while循环...不知何故,我不能让它为我工作。当我在最后输入“否”或“否”时,循环继续运行并且不会停止,直到输入字符串问题的空间。这给了我下面的错误在最后:while循环在netbeans的Java代码
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
at java.lang.Double.parseDouble(Double.java:540)
at mainapp.MainApp.main(MainApp.java:66)
Java Result: 1
BUILD SUCCESSFUL (total time: 41 seconds)
在while语句中放置一个断点并检查s4是否为空 – lateralus 2014-10-16 13:13:16
可能的重复[如何比较Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare -strings-in-java) – EvenPrime 2014-10-16 13:14:41
使用equals而不是== – nobalG 2014-10-16 13:14:43