Results 1 to 4 of 4

Thread: Loop Help!

  1. #1
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default Loop Help!

    Below is the code I have constructed so far for my assignment but am stuck on this one part. The Instructions are in comments.


    Java Code:
    public class MathOperationsTester
    {
        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            
            System.out.println( "TESTING sumOfIntegers"  );
            
            // STEP 3:  The next two statements request the user to enter the value of n and then reads in their response as an int using the
            //      nextInt method of the Scanner class.
            //      The problem with this is that if the user enters something other than an int, we will have a runtime exception.
            //      After requesting the user enters n (first of the next two statements), write a while loop that repeats as long as the user
            //     enters something other than an int (recall that the hasNextInt method of the Scanner class will return true if the user
            //    enters an int and false otherwise.  And recall that ! is Java's not operator.  Inside the loop, you'll want to remove whatever they
            // did enter with a call to the next method of the Scanner class.  And you'll want to give them an error message indicating that they should try again.
            //  This loop should be immediately prior to the call to the nextInt method which has been done for you.
            
            System.out.println( "Enter n:");
            
            // Your loop from Step 3 will go here.
    
    
         while( !hasNextInt())
         System.out.println("Please enter a valid integer");
         
            int n = input.nextInt();
            
            int sum = MathOperations.sumOfIntegers(n);
            System.out.println("Sum of first " + n + " integers is " + sum);

    I have this filled in their so far.


    while( !hasNextInt())
    System.out.println("Please enter a valid integer");


    But obviously taht is not 100% correct. Any help would be great! Thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    it looks like the way hasNextInt() works is it tests to see if the next thing in the scanner is an int, if it is not it will return false., but it also would not clear out that bad input, and the single body of this while loop to print out please enter a valid integer repeats forever right ?

    what about if you loop to
    - display a prompt
    - try to read any input from the user
    - try to see if the input we read is an integer, if so we are done. if not, error, and then continue.

    for example,
    Java Code:
    public class ScannerReadInt {
      public static void main(String[] args) {
      
         Scanner scanner = new Scanner(System.in);
         
         int intValue = 0;
         
         boolean validInput = false;
         
         while (!(validInput)) {
           System.out.print("Please enter a number: ");
           String str = scanner.next();
           try {
             intValue = Integer.parseInt(str);
             validInput = true;
             // or, we could break here too to get out of the loop.
           }
           catch (NumberFormatException ex) {
             // the thing entered is not a valid integer
             System.out.println("Invalid number (" + str + ")");
           }
         }
         
         System.out.println("you entered: " + intValue);
         
      }
    }

  3. #3
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by travishein View Post
    it looks like the way hasNextInt() works is it tests to see if the next thing in the scanner is an int, if it is not it will return false., but it also would not clear out that bad input, and the single body of this while loop to print out please enter a valid integer repeats forever right ?

    what about if you loop to
    - display a prompt
    - try to read any input from the user
    - try to see if the input we read is an integer, if so we are done. if not, error, and then continue.

    for example,
    Java Code:
    public class ScannerReadInt {
      public static void main(String[] args) {
      
         Scanner scanner = new Scanner(System.in);
         
         int intValue = 0;
         
         boolean validInput = false;
         
         while (!(validInput)) {
           System.out.print("Please enter a number: ");
           String str = scanner.next();
           try {
             intValue = Integer.parseInt(str);
             validInput = true;
             // or, we could break here too to get out of the loop.
           }
           catch (NumberFormatException ex) {
             // the thing entered is not a valid integer
             System.out.println("Invalid number (" + str + ")");
           }
         }
         
         System.out.println("you entered: " + intValue);
         
      }
    }
    That looks really good and I appreciate the help but I think its a bit to complex for this assignment. I did try and implement that though and came up with some error. I am not familiar with the try and catch ideas of java yet. Anything a bit more simpler to accomplish what I have to do would be good. I am going to keep messing around with this.

  4. #4
    Keno777 is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default

    Still need a bit of help, I have tried a few different things but no luck. Still stuck with the original loop idea that I had but wont work.

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  2. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 10:15 AM
  3. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:45 PM
  4. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 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
  •