Help with program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help with program

  1. #1
    Join Date
    Feb 2007
    Posts
    1

    Help with program

    If someone could please help with this program. i get it to have me enter the first line of text and then ask for additional items correctly, but when i say for additional items it skips the next input line and goes straight to asking me if I want to enter more items again.

    Code:
    import java.io.*; // uses the java io library for the
    
    import java.util.*; // BufferedReader class
    
    public class week5_701
    
    {
    
    	public static void main(String [] args)
    
    	{
    
    		PrintWriter outputStream=null;
    		try
    
    		{
    			outputStream= new PrintWriter(new FileOutputStream("shoppinglist.txt"));
                    }
                    catch(FileNotFoundException e)
                    {       
                            System.out.println("Error opening the file shoppinglist.txt");
                            System.exit(0);
                    }
                    int items;
                    String line=null;
                    System.out.println("This program will put items in your shopping list");
                    Scanner keyboard= new Scanner(System.in);
                    boolean numbersLeft= true;
                    int moreitems=1;
                    while (numbersLeft)
                    {
                            System.out.println("Enter the next item");
                            line=keyboard.nextLine();
                            System.out.println("Would you like to enter more items, enter 1 for yes or 0 for no");
                            moreitems=keyboard.nextInt();
                            if (moreitems==0)
                               {
                               numbersLeft=false;
                               System.exit(0);
                               }
                            else
                               numbersLeft=true;
                    }
                    
            }
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    there is content in the buffer which is read as a "line" [the "Enter" which the user has pushed - the nextInt method will read only the int and leaves the rest of the "line" in the input buffer]. Try adding a keyboard.nextLine() statement after the nextInt() statement to flush the buffer.

Similar Threads

  1. soft drink Vending machine program
    By divagoddess in forum C++
    Replies: 5
    Last Post: 08-14-2009, 04:12 PM
  2. Help with Server program
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 09:18 AM
  3. Connecting to a Server Program HELP
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 08:58 AM
  4. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM
  5. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 09:28 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center