Results 1 to 4 of 4
Thread: Loop Help!
- 11-03-2009, 10:26 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
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
- 11-03-2009, 10:34 PM #2
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); } }
- 11-03-2009, 10:51 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
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.
- 11-04-2009, 01:11 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 27
- Rep Power
- 0
Similar Threads
-
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
while loop
By Unknown1369 in forum New To JavaReplies: 5Last Post: 07-08-2008, 10:15 AM -
How to use Do While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
while loop
By michcio in forum New To JavaReplies: 5Last Post: 01-27-2008, 12:56 AM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks