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

    }

}

Everything works fine up until the while loop... somehow I cant get it to work for me. When I enter 'No' or 'no' at the end, the loop keeps running and doesn't stop until I enter space for string questions. It just gives me the following error at the end:

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)
spiderman
  • 10,892
  • 12
  • 50
  • 84
lollapalooza
  • 3
  • 1
  • 4

7 Answers7

1

Try:

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

String is an object, not a primitive type so you have to use .equals instead of ==.

RizJa
  • 1,961
  • 1
  • 21
  • 38
0

Use equals method. Strings shouldn't be compared with == or != operator.

peterremec
  • 488
  • 1
  • 15
  • 46
0
while (!"stop".equalsIgnoreCase(s4))

Try this.

Please refer this SO thread for more understanding : Java String.equals versus ==

Community
  • 1
  • 1
spiderman
  • 10,892
  • 12
  • 50
  • 84
0

I belive the following line produces the exception error:

double d2=Double.parseDouble(s6); 

To clarify this, tell us what line is the line 66 on you main method

this happens because the value of s6 is null. try to make sure this is sorted first.

Also as everyone else said, use equals method to check String equality. <--(this won't produce Exception error but it makes your program go wrong semantically).

nafas
  • 5,283
  • 3
  • 29
  • 57
0

Why not use String equals method instead of ==?.For == to work, you would need to call intern() method on s4 which would change the reference to point to String pool.

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

Plaiska
  • 166
  • 2
  • 11
0

Thank you so much everyone for telling me about the .equals method. I had forgotten about that. I submitted my work today after thinking it over and had everything working before I did so...

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

    }
}
lollapalooza
  • 3
  • 1
  • 4
0

I had tried using the .equals() method, it didn't work for me. I think I wasn't writing the loop using the correct format; with the 'count' and all. The 'for' was becoming a disaster too. So I wrote it differently in a way that made sense to me and it ended up working... thanks again to all those who responded!

lollapalooza
  • 3
  • 1
  • 4