Error Checking Loop Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more while loop error checking java about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users how to check two conditions in for loop in java Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping enhanced for loop java null check each other. Join them; it only takes a minute: Sign up Using a do/while loop to error check user input [closed] up vote 0 down vote favorite Hi I am having trouble with a homework assignment. The program
Java Error Checking User Input
is using two different methods to average and display a user defined set of numbers. I figured all of that out, but I am having difficulty with the error checking. I do not want the user to be able to say that they want to average a negative or zero amount of numbers at the beginning so I am trying to use a if/else statement and a do while loop to display an error message to the java while loop user as well as give them the option to try again. When the user enters a 1 to try the program over again after entering an invalid number, the program allows the user to try again. However once they enter everything in correctly and complete the program, the program starts over again. I want the program to end once the program is completed correctly. Any help would be appreciated. Thanks! public static void main(String[] args) { //Defining the variables in main method int inputNumber; int repeat = 0; //Creating the array and checking for negative or no numbers using do..while and if..else do { String aStr = JOptionPane.showInputDialog(null, "How many numbers would you like to be averaged?"); inputNumber = Integer.parseInt(aStr); if(inputNumber <= 0) { String errorStr = JOptionPane.showInputDialog(null, "Cannot be a negative number or a zero. Press 1 to try again."); repeat = Integer.parseInt(errorStr); } else { double[] array = new double[inputNumber]; displayAverage(average(array)); } } while (repeat == 1); } // end main // Creating a method called "average" that calculates and returns the average to main public static double average(double [] methodArray) { // Defining variables in average method int index; double total = 0; double average; // Taking user inputed numbers and adding them up for(index = 0; index < methodArray.length; index++) { String bStr = JOptionPane.showInputDialog(null, "Enter number " + (index + 1)); methodArray[index] = Dou
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Displaying an error message in a Do..While loop in java [closed] up vote -2 down vote favorite http://stackoverflow.com/questions/27096657/using-a-do-while-loop-to-error-check-user-input I'm trying to get my Do..While loop to display an error message to why it is re-asking for the user input again. This is my code for it: do { System.out.println("\nPlease enter the floor you are on: "); current_Floor = in.nextInt(); } // Loop until user input is equal to or less than the maximum floors while( current_Floor > MAX_FLOORS );{ System.out.println("Please enter a floor which is less than 8"); } http://stackoverflow.com/questions/22616825/displaying-an-error-message-in-a-do-while-loop-in-java The problem with this code is that the "Error" message which i've added appears after a correct user input has been entered, when I need it to display before the user input is added in. Any ideas? java loops message do-while share|improve this question edited Mar 24 '14 at 17:49 ArtB 7,1531463104 asked Mar 24 '14 at 17:47 user2863681 125 closed as off-topic by njzk2, Jigar Joshi, BackSlash, mu 無, Adam Smith Mar 24 '14 at 19:08 This question appears to be off-topic. The users who voted to close gave this specific reason:"This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – njzk2, Jigar Joshi, BackSlash, mu 無, Adam SmithIf this question can be reworded to fit the rules in the help center, please edit the question. add a comment| 6 Answers 6 active oldest votes up vote 2 down vote accepted Your formatting is all screwy, which is probably what's confusing you. The ; at the end of your while ends the do-while. The
This Site Careers Other all forums Forum: Java in General How to loop back after an exception is caught? Alan Smith Ranch Hand Posts: 185 I like... posted 4 years https://coderanch.com/t/568303/java/java/loop-exception-caught ago Hi, is there any way to do this? This is seemingly http://codingbat.com/doc/java-for-while-loops.html a very simple task of checking that a user has entered an int and nothing else using the Scanner class (represented by 'sc'). Im writing a card game and am asking the user how many opponents to play against up to a maximum of three. //Ask player for their error checking name and create a player object with their name System.out.println("So whats your name?"); String name = sc.next(); player = new Player(name); //Ask how many opponents to play against System.out.println("Hi " + name + ". How many opponents would you like to play against?"); int numOpponents = sc.nextInt(); //As we are only playing with one deck the maximum number of opponents should be error checking loop three so we don't //run out of cards to deal to everybody. So if the user enter a number greater than three we keep //looping and asking them for a number until they get it right! if (numOpponents > 3) { do { System.out.println("Sorry, did we forget to mention that you can only play against up to three opponents?!"); System.out.println("So how many opponents will it be?"); numOpponents = sc.nextInt(); } while ((numOpponents > 3)); } I know a char is good to use if its menu driven and use a do while until the char is not equal to the exit character eg press 0 to exit. If I could catch the exception on the numOpponent int if someone enters anything but an int and loop back it would be great. If I make a method that calls itself in the catch it just loops back on itself in an endless loop. Any other ideas? edit: heres the method that I wrote to call itself if an exception is caught but it keeps looping if the catch executes and I get a stack
loop practice problems using strings and arrays. Written by Nick Parlante. Java Loop With loops, you get to leverage the power in the computer. Working with computers, you quickly learn that they lack any sort of insight to solve problems on their own. On the other hand, the computer is happy to execute the code we specify a million times over, which is its own sort of power. Loops are the way to do this. A while loop has a boolean test and a body containing statements, like this: int count = 0; while (count < 100) { // test: boolean test within (..) System.out.println("count:" + count); // body: statements within {..} count = count + 1; } System.out.println("all done!"); An if-statement looks at the test one time and then maybe executes the body once. The while-loop extends this idea, executing the body again and again, checking the test each time. The while-loop follows these steps: Check if the test is true or false. If false, the loop "exits" and does not execute the body. If the test is true, continue with step 2. Execute the body statements, starting at the top and proceeding down through them all. Go back to step 1 and check the test again. If it is true, run the body again. Each time the body finishes, "loop around" and check the test again. Each run of the body is called an "iteration" of the loop. Eventually the test is false, the loop exits, and the program continues with the line after the while-loop. For example, the above while-loop prints: count:0 count:1 count:2 count:3 ... count:98 count:99 all done! The count variable starts at 0, and increases by 1 on each iteration. On the 100th iteration of the loop body, the body prints "99" and then increases count to 100. The program loops back to the top and checks the test (count < 100) again. On all previous checks, the test was true. This time the test is false, and the loop exits without executing the body. We use while-loops to execute some code until some condition is satisfied. How To Write a While Loop To write a while-loop, we think about three parts... test -- a boolean test of what should be true before each iteration. Or put another way, the test is the "green light" condition that says that each iteration can go ahead. (The phrase "green light" is a good mnemonic for what the test does.) Eventually, the test should become false and the loop can exit. Think about the precondition that describes the state before each iteration runs -- how are things arranged, what is true? (