• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

"Enter" code key question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks, I tried adding a code where if the user enters nothing, basically pressing the key "Enter", it will save the previous grades inputted and send in the results.Also, if the user presses enter without entering any grades, the program should interpret it as end of input. For example,


This is my code:


What code would I have to use to insert in the code to make it work? Thank you!
 
Sheriff
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm surprised this code compiles. You have lots of code outside methods, including main(), which is not present in your class at all.

Please fix indentation, formatting and re-post the code, so people could read it and possibly help.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main() method is on line 10, Liutauras.

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.
 
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Allowing the user to either enter a float or press Enter is not handled by any of the usual Scanner methods, so it falls on you to write it yourself. You'll need  to call  Scanner#nextLine() to get a String, test for an empty String, and if not empty, then parse the String into a float. This is complicated by the fact that the attempt to parse the string could  throw an exception which you must handle gracefully. This logic is messy enough that people often encapsulated it in some helper method. If the method is supposed to return a float, what do you return if Enter is pressed? In your case, returning -1.0f would work because negative numbers are not allowed.

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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and Welcome to the Ranch!  
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few things about Scanner:

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.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt OP is at a point where Optional is something she can consider using.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like the only difference between checking for Optional.isPresent() vs (value == FLAG) is the mechanics. I'd probably see if there was a different way to structure the logic, maybe with a check for hasNextDouble() first before deciding to either call nextDouble() or check if the nextLine() is empty to end.
 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey's solution shows how failing to get an input is inconsistent with the design concept behind Scanner. A Scanner pointing to System.in implicitly always has a next token. If you keep going until you get an empty line, you will stop but using parseDouble() on each line of input means you are missing out the power of nextDouble(). If you are going to parse things like that, why not use the old technique of a buffered reader pointing to System.in?
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.
 
Carey Brown
Bartender
Posts: 10979
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Try it. You can hit Enter till your blue in the face but it never leaves the hasNextDouble() method.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, detecting one ENTER is kind of tricky. This works but you have to hit ENTER twice:

Edit:  If you use input.next() on line 11, it works even better because you can enter multiple numbers on a single line and the program will read all of them. The program will also terminate once it encounters any non-numeric character in the input stream so you can instruct the user to "type '$' or hit <ENTER> twice to end" or something like that.

*** 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.
 
Junilu Lacar
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found an incantation that works the way I described above: You have to hit ENTER twice to get it to stop. It will also stop if you enter anything that isn't a number, like "$" or "END"

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
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic