2014-10-16 84 views
0
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) 
+0

在while语句中放置一个断点并检查s4是否为空 – lateralus 2014-10-16 13:13:16

+1

可能的重复[如何比较Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare -strings-in-java) – EvenPrime 2014-10-16 13:14:41

+0

使用equals而不是== – nobalG 2014-10-16 13:14:43

回答

0

为什么不使用String equals方法而不是==?为了工作,您需要在s4上调用intern()方法,这会将引用更改为指向字符串池。

而(!s4.equals( “停止”))

1

尝试:

while(!s4.equals("stop")){ 

字符串是一个对象,而不是原始的类型,因此你必须使用.equals而不是==。

0

使用等号方法。不应将字符串与==或!=运算符进行比较。

0

我相信下面的行产生异常错误:

double d2=Double.parseDouble(s6); 

为了澄清这一点,告诉我们什么行是行66你主要方法

发生这种情况是因为s6的值为null。 尝试确保这是首先排序。

也如其他人所说,使用equals方法检查String是否相等。 < - (这不会产生异常错误,但它会让你的程序在语义上出错)。

0

非常感谢你对每个人都告诉我了.equals方法。我已经忘记了这一点。我今天提交我的工作,思前想后,不得不一切之前,我做了这样的工作...

package mainapp; 

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); 



int count=0; 
while(count>=0){ 

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); 
count++; 

String s8=JOptionPane.showInputDialog("Would you like to add more items to your cart?"); 
if(s8.contains("Yes")|| s8.contains("yes")){ 

} 

else{  
String s7=JOptionPane.showInputDialog("Please enter 'Stop' if you are done adding items to your cart"); 
if (s7.contains("stop")|| s7.contains("Stop")){ 
    count=-1; 
} 

} 
} 
JOptionPane.showMessageDialog(null, 
     cart.toString(), 
     "Cart Details", 
     JOptionPane.INFORMATION_MESSAGE); 

    } 
} 
0

我已经使用.equals()方法试过了,它没有为我工作。我认为我没有使用正确的格式编写循环;与“数”和所有。 'for'也变成了一场灾难。所以我以一种对我有意义的方式以不同的方式写下了它,并且它最终结束了工作......再次感谢所有那些回应!