Results 1 to 8 of 8
  1. #1
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Problem with loop

    Hey everyone! I haven't been programming long and I am in a java programming class now and we are moving to working on repetition statements.

    The program that we are working on takes 3 different input and displays them for the user. It only runs once though and then you have to restart the program.

    We are suppose to have them answer the question "Do you have any more books to input?", with either "yes" or "no".

    I understand the general concept of loops and I can get the user to input either yes or no, but after the first loop, when the question is asked and yes is input, when it comes back to asking if they have more to enter it skips letting the user input yes or no. I think I read somewhere that if a string is assigned to a variable it can't be changed.

    If this is the case, how can I make it where when they enter yes it continues the loop, but then when it comes back around it lets the user make another input?

    If needed I can post the code.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Without code there is little hope that we can guess what you are doing wrong, so yes, please post code. Also, please read my signature below about use of code tags. Thanks and good luck.

  3. #3
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    The program works fine, it just won't let the user answer the question the second time around, it prints it but doesn't allow input.


    Java Code:
    import java.util.Scanner; // Program uses class scanner
    import java.io.InputStream; // Program uses scanner input
    
    public class LFCtwo	//start of class LFC
    {
    	//begin method main
    	public static void main( String args[] )
    	{
    		Scanner input = new Scanner( System.in );
    		
    		String cmdEnd = "";
    		boolean end = true;
    		int  daysLate;		// Create integer for days
    		float dailyFine;	// Create float for daily charge
    		float fineAmount;	// Create float for fine
    		
    		// Welcome user
    		System.out.println( "Welcome to the Library Fine Calculator.\n" );
    		
    		while ( end ) {
    			
    			System.out.println( "Do you have any more items to check in? (Yes or No)\n" );
    			cmdEnd = input.nextLine();
    			
    			if ( cmdEnd.equalsIgnoreCase("No") ) {
    				break;
    			}
    			
    			// Get book input
    			System.out.println( "Please enter the name of the Book being returned:" );
    			String bookName = input.nextLine(); //Store bookName as string
    			
    			// Get days input
    			System.out.println( "\nPlease enter the number of days the book is late:" );
    			daysLate = input.nextInt(); // Store into int daysLate
    			
    			// Get fine amount
    			System.out.println( "\nPlease enter the per day fine for the book:" );
    			dailyFine = input.nextFloat(); // Store into fineAmount
    			
    			fineAmount = daysLate * dailyFine;
    			
    			// Print all related information.
    			System.out.printf( "\n   Returned Book: %s\n   This book was %d days overdue.\n   The amount owed is $%.2f\n" , bookName , daysLate , fineAmount );
    			
    			System.out.println();
    			
    			
    		}
    		
    	} // end method main
    	
    } // end class LFC

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What happens if after any call to input.nextInt() and input.nextFloat(), you call input.nextLine()?

    i.e.,:
    Java Code:
    daysLate = input.nextInt();
    input.nextLine()
    Incidentally, you should avoid using float when you can use a double as the accuracy differences are significant.

  5. #5
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Yeah I got the double / float differences and such and in this case I'm sure it won't differ to greatly. If we have any future assignments with this same program then I might change it, but I'm just concerned with it not allowing the input.nextLine(); to repeat on the second time through.

    As far as if there is a significant difference in using nextLine before or after the other input, I've tried both before and after and still can't get it to work.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My suggestion is correct. Let's see how you've implemented it. Again (and as always), please show the code.
    Last edited by Fubarable; 10-04-2009 at 10:15 PM.

  7. #7
    Tykk is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    It worked this time, but if you don't mind explaining why it works like this, I would appreciate it.

    Java Code:
    import java.util.Scanner; // Program uses class scanner
    import java.io.InputStream; // Program uses scanner input
    
    public class LFCtwo	//start of class LFC
    {
    	//begin method main
    	public static void main( String args[] )
    	{
    		Scanner input = new Scanner( System.in );
    		
    		String cmdEnd = "";
    		boolean end = true;
    		int  daysLate;		// Create integer for days
    		double dailyFine;	// Create float for daily charge
    		double fineAmount;	// Create float for fine
    		
    		// Welcome user
    		System.out.println( "Welcome to the Library Fine Calculator.\n" );
    		
    		while ( end ) {
    			
    			System.out.println( "Do you have any more items to check in? (Yes or No)\n" );
    			cmdEnd = input.nextLine();
    			
    			if ( cmdEnd.equalsIgnoreCase("No") ) {
    				break;
    			}
    			
    			// Get book input
    			System.out.println( "Please enter the name of the Book being returned:" );
    			String bookName = input.nextLine(); //Store bookName as string
    			
    			// Get days input
    			System.out.println( "\nPlease enter the number of days the book is late:" );
    			daysLate = input.nextInt(); // Store into int daysLate
    			input.nextLine();
    			
    			// Get fine amount
    			System.out.println( "\nPlease enter the per day fine for the book:" );
    			dailyFine = input.nextDouble(); // Store into fineAmount
    			input.nextLine();
    			
    			fineAmount = daysLate * dailyFine;
    			
    			// Print all related information.
    			System.out.printf( "\n   Returned Book: %s\n   This book was %d days overdue.\n   The amount owed is $%.2f\n" , bookName , daysLate , fineAmount );
    			
    			System.out.println();
    			
    			
    		}
    		
    	} // end method main
    	
    } // end class LFC

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Tykk View Post
    It worked this time, but if you don't mind explaining why it works like this, I would appreciate it.
    Great, glad it worked. The problem is that nextInt() and next() don't "swallow" the end of line character leaving it dangling. Your next call to nextLine() will swallow this and doesn't allow you to input your "yes" or "now". To fix this, deal with the end of line character when necessary.

Similar Threads

  1. if else loop problem
    By Ms.Ranjan in forum New To Java
    Replies: 12
    Last Post: 04-25-2009, 09:30 AM
  2. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  3. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM
  4. Problem to use different for loop to add up
    By matt_well in forum New To Java
    Replies: 6
    Last Post: 08-03-2008, 10:24 PM
  5. For loop problem
    By mcal in forum New To Java
    Replies: 32
    Last Post: 01-25-2008, 03:51 PM

Tags for this Thread

Posting Permissions

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