Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Scanner not looping.

  1. #1
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question Scanner not looping.

    In my Exception Throwing assignment we were given a code to edit, and told to make a loop within the numberFromUser method. A Scanner should input a new value until its value equals a integer that is not zero, in which case it would solve 7 % x where x is the value imputed.

    My problem is my loop isn't working, I was able to get it to work in the main method, however in the instructions they specifically say they want the loop in the numberFromUser method.

    This is my code, and everything works except for the do, while loop.
    package exception.handling;
     
    import java.util.InputMismatchException;
    import java.util.Scanner;
     
    public class LabExceptionHandilng
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    		int digit = numberFromUser();
    		int result = sevenModulusN(digit);
    		System.out.printf("7 %% %d = %d", digit, result);
    		}
    		catch(IllegalArgumentException e)
    		{
    			System.out.println("A problem occurred: 7 % 0 is an invalid operation!");
    		}
    		catch(InputMismatchException e)
    		{
    			System.out.println("A problem Occured: The number entered needs to be a whole number.");
    		}
     
    	}
     
    	private static int numberFromUser()
    	{
    		Scanner input = new Scanner(System.in);
    		boolean done = false;
    		Integer value = 1;
     
    		do
    		{
    			System.out.print("number: ");
    			value = input.nextInt();
     
    			if(value instanceof Integer && value != 0)
    				done = true;
    			if(value == 0)
    				throw new IllegalArgumentException("7 % 0 is a bad operation!");
     
     
    		}while(!done);
     
    		return value;
    	}
     
    	private static int sevenModulusN(int number)
    	{
    		return 7 % number;
    	}
    }

    This is the code given to us prior to edit.
    import java.util.Scanner;
     
    public class LabExceptionHandilng
    {
    	public static void main(String[] args)
    	{
    		int digit = numberFromUser();
    		int result = sevenModulusN(digit);
    		System.out.printf("7 %% %.1f = %.1f", digit, result);
    	}
     
    	private static int numberFromUser()
    	{
    		Scanner input = new Scanner(System.in);
    		System.out.print("number: ");
    		return input.nextInt();
    	}
     
    	private static int sevenModulusN(int number)
    	{
    		return 7 % number;
    	}
    }

    I have a feeling, I'm overlooking something but I cant figure it out. Any help would be much appreciated.
    Last edited by Spencer4908; July 21st, 2014 at 02:32 PM. Reason: Question was solved.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,360
    My Mood
    Mellow
    Thanks
    254
    Thanked 786 Times in 771 Posts

    Default Re: Scanner not looping.

    Define, "My problem is my loop isn't working." Give a sample run and describe what's wrong with it.

  3. #3
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: Scanner not looping.

    It's to understand what you're trying to do here but your while loop works, once.
    And it will run once as long as value is not equal to 0 in which case you will throw an exception.

    Are you trying to get the value such that 7%x == 0? If so then replace value !=0 with 7%value !=0

    Lastly, the while, the if and even for loop run on true statements while(true){}, if(!false), for(i=0;true;i++){} will run, while(false), e.t.c wont.

  4. #4
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Scanner not looping.

    Well, the output should loop if the user inputs a value that throws an exception. For example if they were to enter a float, String or a zero the value would return an exception and loop back to allow the user to re-enter a new value.

    However, no matter what value I put in, it doesn't loop back to allow the user to re-input a new value.

    Example of my Output:
    enter number: 2.7
    A problem Occurred: The number entered needs to be a whole number.


    Example of how it should be:

    enter number: 2.7
    A problem Occurred: The number entered needs to be a whole number.
    enter number:


    keep in mind the green text is user imputed info

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,360
    My Mood
    Mellow
    Thanks
    254
    Thanked 786 Times in 771 Posts

    Default Re: Scanner not looping.

    Ahhh. The catch block could be used to call the numberFromUser() method again, but that's not the best approach. Instead, I suggest you rethink your design.

    I recommend you start with your approaches for handling errors. You are trying to catch two errors, 1) input other than a whole number (might need adjusting), and 2) invalid modulo operation. Error handling should occur as close to the error as possible, so user input errors should be handled in the numberFromUser() method and invalid modulo operations should be captured in the sevenModulusN() method.

    Move the error handling to those two methods, catching the appropriate errors in each, and remove any error handling from the main() method. If the main() method is supposed to repeat until the user signals a desire to quit, then add a loop to the while method that repeats.

  6. #6
    Junior Member Spencer4908's Avatar
    Join Date
    Jul 2014
    Location
    UTAH, USA
    Posts
    12
    My Mood
    Cheerful
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Scanner not looping.

    Thank you for you help, I managed to find a solution. I miss interpreted some of the assignments direction, causing a bit of confusion on my part. Thanks again, your advice helped a lot.

Similar Threads

  1. Need help with looping
    By zizo911 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 24th, 2014, 08:02 PM
  2. DO WHILE LOOPING
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 22nd, 2013, 10:57 AM
  3. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  4. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 02:32 PM