Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I ran into a similar problem not too long ago. You will need to validate your input before you store it. To do that, it is important to visualize the scanner has a "pipe" with a string going through the entrance of the pipe (user input) and different possible values (just a text string, an int, a double, a binary, or other formats) "coming out" from the other end of the pipe (which is your code reads the scanner in your code.)</p> <p>Here is the list of validations that it seems like you want to implement:</p> <ul> <li>is the length of the next value coming through greater than 0? (this is the same as asking if the user actually entered a something or nothing before pressing Enter. It is done automatically by the scanner.Next() function in general.)</li> <li>is the next value is a number?</li> <li>if the next value is not a number, is there a number down the pipe? (this is the same as checking to see if the user might have inserted a space before the number by mistake.)</li> </ul> <p>I have written below an example of what should happen each time you are looking for one of your grades:</p> <pre><code>import java.io.*; import static java.lang.System.*; import java.util.Scanner; class Main{ static double t1, t2, q1, q2, q3, hmw; public static void main (String str[]) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Please enter your test grades."); System.out.println("Enter grade one: "); // While there is something in the pipe while(scan.hasNext()!=false) { // Check if this value is an integer if(scan.hasNextInt()) { t1 = scan.nextInt(); System.out.println("Successful input. Grade entered: "+t1); break; } // it is not an integer, display a message saying it is an invalid input else { // Flush the invalid input scan.next(); System.out.println("Invalid input detected."); } } //... (rest of the code) </code></pre> <p>Remember that, in this example, if you enter [any amount of garbage non number text] + space + an integer, it will scroll through the entire thing and find the integer.</p>
    singulars
    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.
    1. VO
      singulars
      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