This is my code:
What code would I have to use to insert in the code to make it work? Thank you!
Please fix indentation, formatting and re-post the code, so people could read it and possibly help.
This shows why it's important to format your code properly. It's easier for others to see where methods start and end when you add a blank line before and after the method. This gives it some visual separation from the rest of the code. Proper indentation and alignment also helps others see which statements belong together in a block like inside the body of an if or while statement.
For example, the closing brace on line 59 is aligned with the if-statement on line 26. This misleads the reader into thinking that lines 27-58 are part of the if-statement's body when in fact, line 30 ends the if-statement body making line 27 the only statement in it. The comment at the end of line 59 is misleading as well in that it says "ends class" when in fact, it only ends the main() method. The closing brace for the class is on line 62. Proper alignment and indentation would have made the "ends class" comment redundant and unnecessary.
Normally I don't like to give out solutions but this is not an easy problem, especially for beginners.
Then to call the helper method you have something like
Some other comments on your code: There are some rare cases where you need to use a type of float; all other times you should use double instead. So you'll need to go through your code and tweak it a bit to make that change.
Another point is that a Scanner of System.in should only be opened once in your code and then shared with any methods that need it. The code you posted opens it once (good), but does not share it (bad). This becomes an issue when you have a helper method that needs access to it. The fix for this is to create a Scanner that is a "static final Scanner" for the class, then the helper method can see it.
Christia Li wrote:What code would I have to use to insert in the code to make it work? Thank you!
A little experimentation with the various nextXXX() methods of the Scanner class goes a long way here.
Here's a small program you can use to experiment:
Change line 7 so that the type of the variable matches the type returned by whatever nextXXX() method of Scanner you're trying out.
1. Notice how I declared and initialized the Scanner as a static field of the class, outside of the main() method. You only have to call new Scanner(System.in) once when you run your program and the best way to do that how I showed it. Doing it inside main() complicates things when you start breaking out your program into smaller methods (it's not good practice to put all your code in main()).
2. You named your Scanner variable Gscanner. First, this name does not follow Java conventions. The Java convention has variable and method names start with a lowercase letter. Type names (classes, interfaces, enums, and generic parameter types) start with uppercase letters. Mixing these up makes your code confusing to read. Also, the name itself should reflect the purpose, not the implementation. Names like input or keyboard make your program more readable. When choosing names, try to communicate intent/purpose, not implementation.
Carey Brown wrote: In your case, returning -1.0f would work because negative numbers are not allowed.
Isn't this a good use-case to use java.lang.Optional instead of returning a -1 ?
Junilu Lacar wrote:I doubt OP is at a point where Optional is something she can consider using.
Possible. But using the same field to return Data or a a special meaning flag (-1) is something I would not prefer. The code calling the inputGrade method will have to decipher it's result to determine if it was a number or a flag.
I like Junilu's solution: if you enter “stop” the hasNextDouble() test will fail and your loop will terminate cleanly.
By the way: Don't do arithmetic with floats. If I had my way, nobody would ever do anything with floats. If you want to do folating‑point arithmetic, use doubles.
Try it. You can hit Enter till your blue in the face but it never leaves the hasNextDouble() method.
*** IGNORE the above code ***
I had only tested with single digit numbers as input. Doesn't seem to work when you enter anything greater than 9.
Looks like it will stop right away if you type in a SPACE then enter.
Play around with it here: https://repl.it/Nuxm/9