Results 1 to 3 of 3

Thread: Java Loop help

  1. #1
    Join Date
    Nov 2009
    Posts
    583

    Java Loop help

    Hi
    I have to complete an assignment, for which I am trying to write a program. I have posted my code below and all the instruction are in the comments.
    Code:
    public class MathOperationsTester
    {
        public static void main(String[] args)
        {
            Scanner in = 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 = in.nextInt();
            int sum = MathOperations.sumOfIntegers(n);
            System.out.println("Sum of first " + n + " integers is " + sum);

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Java Loop help

    It seems hasNextInt() is a test to see if the next thing in the scanner is an int, and the single body of this while loop to print out please enter a valid integer repeats forever right ?
    I suggest you for the loop to
    1) display a prompt
    2) try to read any input from the user
    3) try to see if the input we read is an integer, if so we are done.
    Code:
    public class ScannerReadInt {
      public static void main(String[] args) {
           Scanner sc= new Scanner(System.in); 
         int intv = 0;
         boolean validInput = false;
         while (!(validInput)) {
           System.out.print("Please enter a number: ");
           String str = scanner.next();
           try {
             intv = 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: " + intv);
      }
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Java Loop help

    Thanks for the help
    That looks really good and I appreciate you work, but I think the code is bit of complex for this assignment. I tried it and implemented it, but I came to some errors. I think I am not familiar with the try and catch ideas of java yet. Anything more easy would do for me, that would be simpler for my assignment. But I am going to keep messing around this code and codes like these so I can learn java quickly. Thanks for you help again.

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 07:23 PM
  3. How to use the do-while loop statement in java program?
    By KALIDA in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 07:53 PM
  4. Differentiate between Do-While loop and While loop
    By REDBULL in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 10:10 AM
  5. Java for loop example
    By NetworkeR in forum Software Development
    Replies: 2
    Last Post: 02-11-2009, 12:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,033,586.98372 seconds with 16 queries