Results 1 to 6 of 6
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default I'm so lost, trying to disable a loop or continue it on users input

    I can't figure this out. So, here's the question I'm being asked to solve.

    Repeat the Lower Bound/Upper bound lab from last time, except that you are to put a while
    loop around it so that the user is asked at the end :

    "Do you want to do this again? (y/n)"

    If the user enters a y, then you repeat. This continues until the
    user enters an n.
    Now this is my Low Bound/Upper Bound code.

    Java Code:
    import java.util.Scanner; 
    
    public class NumberBounds {
    
        public static void main(String[] args)  {
            Scanner keyboard = new Scanner(System.in);
    
            System.out.println("Enter a number");
            int starting_number = keyboard.nextInt();
    
            System.out.println("Enter a Upper Bound");
            int upper_bound = keyboard.nextInt();
    
            System.out.println("Enter a addition increment");
            int step_size = keyboard.nextInt();
            starting_number += step_size;
    
            int count = 0;
    
            while (starting_number <= upper_bound) {
                System.out.print(starting_number + " ");
                starting_number += step_size;
    
                count += 1; if ((count % 10) == 0)
                    System.out.println();
            }
    
    
        }
    }
    And this is my attempt to adding the "Do you want to do this again? (y/n)", that's asked in my homework. In this code it's now skipping the entire, which it isn't suppose too.

    Java Code:
    while (starting_number <= upper_bound) {
    				System.out.print(starting_number + " ");
    				starting_number += step_size;
    
    				count += 1; if ((count % 10) == 0)
    					System.out.println();
    			}
    section and I can't seem to make the (Y/N) work. I want it to where if the user enters a Y then it goes to (else) and continues the program, but if they enter a N then it takes the 1 which is assigned to "char N = 0" and minus's 1 making it 0, thus making the continueflag false and discontinuing the program.

    Java Code:
    import java.util.Scanner; 
    
    public class NumberBounds {
    
    	public static void main(String[] args)  {
    		Scanner keyboard = new Scanner(System.in);
    		boolean continueflag = true;
    		char N = 1, Y = 5;
    
    		do {
    
    			System.out.println("Enter a number");
    			int starting_number = keyboard.nextInt();
    
    			System.out.println("Enter a Upper Bound");
    			int upper_bound = keyboard.nextInt();
    
    			System.out.println("Enter a addition increment");
    			int step_size = keyboard.nextInt();
    			starting_number += step_size;
    
    			int count = 0;
    
    			while (starting_number <= upper_bound) {
    				System.out.print(starting_number + " ");
    				starting_number += step_size;
    
    				count += 1; if ((count % 10) == 0)
    					System.out.println();
    			}
    
    
    			{
    				System.out.println("Would you like to continue (Y/N)");
    
    				if (N == 0)
    					System.out.println("The program has been terminated");
    				N--;
    				continueflag = false;
    
    				if (Y >= 1)
    					Y++;
    			}
    
    		}
    
    		while (continueflag != false);
    
    
    	}
    }

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: I'm so lost, trying to disable a loop or continue it on users input

    I would do something like this:

    Java Code:
    Scanner keyboard = new Scanner(System.in)
    while(true)
    {
    	//other logic here
    	
    	System.out.println("Continue (y/n)?");
    	String cont = keyboard.nextLine().toLowerCase();
    	if(cont.startsWith("n")) //if the user entered n
    		break; //break out of (end) the while loop
    }
    //goes to this line after the "break" statement
    Hope this helps.
    Last edited by AndrewM16921; 02-16-2013 at 06:25 PM.

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: I'm so lost, trying to disable a loop or continue it on users input

    So, this is what I placed into the code you gave me.

    Java Code:
    import java.util.Scanner;
    
    
    public class factorial {
    
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		while(true)
    		{
    			while (true){
    				int Factorial = 1, Number;
    				{
    				System.out.println("Enter a value for N");
    				Number = keyboard.nextInt();
    				}
    				for (int K = 1;K <= Number; K++)
    				{
    					Factorial *= K;
    				}
    				System.out.println("The Factorial of "+Number +" is "+ Factorial);
    				
    				System.out.println("Continue (y/n)?");
    				String cont = keyboard.nextLine().toLowerCase();
    				if(cont.startsWith("n"))
    					break; 
    			}
    		}
    
    	}
    }
    But this is what my output is

    Java Code:
    Enter a value for N
    6
    The Factorial of 6 is 720
    Continue (y/n)?
    Enter a value for N
    y
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Unknown Source)
    	at java.util.Scanner.next(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at java.util.Scanner.nextInt(Unknown Source)
    	at factorial.main(factorial.java:14)

  4. #4
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: I'm so lost, trying to disable a loop or continue it on users input

    I figured it out

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: I'm so lost, trying to disable a loop or continue it on users input

    First, I would recommend getting rid of the outer while(true) loop. I don't believe you need it. Second, apparently there is still
    some residual characters in the input stream from your first use of keyboard.nextInt(). So put a keyboard.nextLine() right after
    it and see what happens.

    Jim

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: I'm so lost, trying to disable a loop or continue it on users input

    Sorry about that. The problem is that when you use Scanner.nextInt(), the user enters this with the "enter" key, which leaves a newline character. You need to "consume" this new line by calling nextLine() right after. Also, you have two while loops, you should only have one in this case. I provided a modified version of your code below.
    Java Code:
    import java.util.Scanner;
    
    public class factorial
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		while(true)
    		{
    			int Factorial = 1, Number;
    			{
    				System.out.println("Enter a value for N");
    				Number = keyboard.nextInt();
    				keyboard.nextLine();
    			}
    			for(int K = 1; K <= Number; K++)
    			{
    				Factorial *= K;
    			}
    			System.out.println("The Factorial of " + Number + " is " + Factorial);
    			
    			System.out.println("Continue (y/n)?");
    			String cont = keyboard.nextLine().toLowerCase();
    			if(cont.startsWith("n"))
    				break;
    		}
    		keyboard.close();
    	}
    }

    Also, for the sake of convention, class names are usually CapitalizedCamelCase, and variables are usually lowerCamalCase. This technically doesn't matter, but it may be a tad bit confusing for others reading your code. You seem to most of these switched in your code.
    Last edited by AndrewM16921; 02-16-2013 at 07:30 PM.

Similar Threads

  1. continue outside of loop...
    By J-max04 in forum New To Java
    Replies: 12
    Last Post: 05-04-2012, 02:48 AM
  2. Interrupt loop without break or continue
    By mwr1976 in forum New To Java
    Replies: 2
    Last Post: 10-12-2011, 04:43 AM
  3. help me for this nested for loop with users input
    By jakemanalaotao in forum New To Java
    Replies: 4
    Last Post: 09-06-2011, 04:50 PM
  4. Replies: 2
    Last Post: 02-22-2011, 04:07 AM
  5. problem with Scanner in Getting users input
    By kliane in forum New To Java
    Replies: 8
    Last Post: 01-17-2010, 05:37 PM

Posting Permissions

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