|
| |||||||||
| Tags: for, java, loop, programming language, while |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| ||||
| ||||
| 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
| |||
| |||
| 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. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Java Loop help" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Watercooling: Single loop or Dual Loop | Akolekar | Hardware Peripherals | 3 | 21-10-2011 11:52 PM |
| Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work | mADiRAkSHii | Operating Systems | 4 | 25-01-2011 07:23 PM |
| How to use the do-while loop statement in java program? | KALIDA | Software Development | 5 | 27-01-2010 07:53 PM |
| Which is best for iterator: For loop or while loop | Leeland | Software Development | 4 | 18-01-2010 06:03 PM |
| Java for loop example | NetworkeR | Software Development | 2 | 02-11-2009 12:15 PM |