Note that there are some explanatory texts on larger screens.

plurals
  1. POtry statement not being used on second illiteration of loop
    primarykey
    data
    text
    <p>I am making a method in order to get how many numbers the user wants to sum together. aka if they want to sum the numbers 1 2 and 3. they would want to sum 3 numbers together. So when i ask them how many they want to sum together, I use a try - catch in order to catch if they enter a decimal place. Because you cant add together 3.5 numbers you can add 3 numbers or 4. problem is if the user enters a decimal, the program will infinite loop run everything but what is in the try statement. How can i fix this?</p> <p>Here is the code for the method:</p> <pre><code>private static int requestMaxInputForFloat(Scanner scanner){ boolean appropriateAnswer = true; // assume appropriate catch not appropriate to loop again int howManyInputs = 1000000; // hold value to return how many inputs. if this value we will not run. //request an appropriate number of inputs until appropriate answer = true; do { appropriateAnswer = true; //if looped again reset value to true try{ System.out.print("How many decimal place numbers would you like to sum? "); howManyInputs = scanner.nextInt(); }catch(Exception e){ System.out.println("Sorry but you can only request to average a whole number set of data.\nTry Again."); appropriateAnswer = false; }//end of try-catch if (howManyInputs &lt;= 0) //invalid answer { System.out.println("Sorry but " + howManyInputs + " is equal to or below 0. Please try again."); }else{ appropriateAnswer = false; } }while(!appropriateAnswer);//end of while loop return howManyInputs; //return the value }// end of getMaxInput </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload