Results 1 to 5 of 5
  1. #1
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default 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;
    							}
    																	
    					}
    		}
    	}
    }

  2. #2
    dnzzn is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    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:

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by dnzzn View Post
    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:
    Read the API specs for the next and nextLine methods to see the difference.

  5. #5
    moamen is offline Member
    Join Date
    Sep 2009
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by dnzzn View Post
    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:
    Thanx it worked:D
    but i wonder what was the problem in the former code ??:confused:
    but thanx very much for your help!!


    Quote Originally Posted by Eranga View Post
    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?
    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

  1. Replay Solutions Job Fair - Java & C++ Jobs
    By jackchang in forum Jobs Offered
    Replies: 0
    Last Post: 08-19-2008, 06:17 PM
  2. Issue using FOR statment with NetBeans
    By Deathmonger in forum Advanced Java
    Replies: 3
    Last Post: 07-30-2008, 12:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •