Results 1 to 5 of 5
Thread: statment replay!!!
- 09-25-2009, 01:52 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 15
- Rep Power
- 0
statment replay!!!
Hi Every one,
i'm new to java programming
and while i was trying to make the following programm a problem occured
the problem is that the statment
"Please choose the item you want to buy"
repeats twice every time the loop repeats
so please anyone give me a hand to fix this
thanx
Java Code:import java.util.Scanner; public class Store2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String item1 = "Adidas shoes"; String item2 = "Nike shirt"; String item3 = "Anta wallet"; String choose = ""; Scanner input = new Scanner(System.in); String dicide = ""; double total2 = 0; int qty1=0; int qty2=0; int qty3=0; double total; System.out.printf("We have three items\n1-%s\n2-%s\n3-%s\n\n",item1,item2,item3); while((!choose.equals("stop"))) { System.out.print("Please choose the item you want to buy: \n"); choose = input.nextLine(); if((choose.equals("1"))) { System.out.printf("You have chose \"%s\"\n",item1); System.out.printf("Are you sure u want to buy \"%s\"?\n",item1); dicide = input.nextLine(); if(dicide.equals("yes")) { System.out.println("How many pieces you wanna buy?"); qty1 = input.nextInt(); System.out.println("the price per piece is $229.88"); System.out.printf("The total price is: $%.2f\n",total = qty1*229.88); total2 = total2 + total; } } } } }
- 09-25-2009, 02:23 AM #2
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
well i dont actually know why, but changing nextLine() to next() in lines 27 and 32 solves the problem. yet i still dont understand why the code above doesnt work properly! :confused:
- 09-25-2009, 06:37 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Before that actually once I looking into your while loop, seems to me it's not quite for other numbers except three items you define. Isn't it?
- 09-25-2009, 08:20 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-25-2009, 12:49 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 15
- Rep Power
- 0
Thanx it worked:D
but i wonder what was the problem in the former code ??:confused:
but thanx very much for your help!!
my loop is just for the three items i defined and the former code is a part of the programme not the whole prog.
i hope i understood you correctely!!!;)
Similar Threads
-
Replay Solutions Job Fair - Java & C++ Jobs
By jackchang in forum Jobs OfferedReplies: 0Last Post: 08-19-2008, 06:17 PM -
Issue using FOR statment with NetBeans
By Deathmonger in forum Advanced JavaReplies: 3Last Post: 07-30-2008, 12:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks