while loop in Java code for netbeans -



while loop in Java code for netbeans -

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 come in quantity of product have selected"); int r = integer.parseint(s1); string s2 = joptionpane .showinputdialog("please come in name of product"); string s3 = joptionpane .showinputdialog("please come in cost of product"); double d1 = double.parsedouble(s3); (int = 0; < r; i++) { product p1 = new product(); p1.setname(s2); p1.setprice(d1); cart.add(p1); } double total_price = 0; (int = 0; < cart.size(); i++) { total_price = total_price + cart.get(i).getprice(); } joptionpane.showmessagedialog(null, cart.tostring() + "the total cost of products in cart : " + total_price + " kd", "shopping information", joptionpane.information_message); cart.removeall(cart); joptionpane.showmessagedialog(null, "thank shopping us! ", "prompt", joptionpane.information_message); string s4 = joptionpane .showinputdialog("would add together items cart"); while (s4 != "stop") { string s5 = joptionpane .showinputdialog("please come in name of product"); string s6 = joptionpane .showinputdialog("please come in cost of product"); double d2 = double.parsedouble(s6); product p2 = new product(); p2.setname(s5); p2.setprice(d2); cart.add(p2); string s7 = joptionpane .showinputdialog("would add together more items?"); if (s7 == "no" || s7 == "no") s4 = "stop"; } joptionpane.showmessagedialog(null, cart.tostring(), "cart details", joptionpane.information_message); } }

everything works fine until while loop... somehow cant work me. when come in 'no' or 'no' @ end, loop keeps running , doesn't stop until come in space string questions. gives me next error @ end:

exception in thread "main" java.lang.nullpointerexception @ sun.misc.floatingdecimal.readjavaformatstring(floatingdecimal.java:1008) @ java.lang.double.parsedouble(double.java:540) @ mainapp.mainapp.main(mainapp.java:66) java result: 1 build successful (total time: 41 seconds)

why not utilize string equals method instead of ==?.for == work, need phone call intern() method on s4 alter reference point string pool.

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

java loops netbeans while-loop

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -