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 ... как-то я не могу заставить его работать на меня. Когда я вхожу «No» или «no» в конец, цикл продолжает работать и не останавливается, пока я не введу пространство для вопросов с строкой. Она просто дает мне следующую ошибку в конце:while loop in Java code for netbeans
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
Возможный дубликат [Как сравнить строки в Java?] (Http://stackoverflow.com/questions/513832/how-do-compare -strings-in-java) – EvenPrime
use equals, а не == – nobalG