Forums Register Login
What is returned when a user presses just the enter key
Name: Sherman
Platform: HP Compaq 6710b
Operating System: Ubuntu 17.10
postgresql version: 9.6
openjdk version: 9
javac version: 1.8

How can I tell if a user has just pressed the Enter key? What kind of return will I get?

Thank you;

Maybe since this is my first post I should introduce myself. I am 71, retired, and creating my own postgresql poker statistics database. I am familiar with PERL and I have never tried coding in C, C++, java, or other oop languages. I am not that familiar with oop so maybe my terminology within the group will be wrong, and I apologize for that. I am doing this in java as a learning tool but with a positive script that I can actually use.

Thank you;

Welcome to CodeRanch Sherman.

Are you able to post any of the code that you are working with? If so please use the code tags as mentioned here https://coderanch.com/wiki/659781/Code-Tags.
I'm pretty that you can get returned value from press Enter within Swing or AWT of JavaFX. However if you are starting with Java you may want to work you way up to that.

If you are not using Swing or AWT or JavaFX then you are most likely going to use the Scanner object.
If this is the case then I would suggest that you do something like
"Are you sure you want to continue y/n"
and then grab the next character using nextString.

You can read more about this object here https://docs.oracle.com/javase/9/docs/api/java/util/Scanner.html
Thank you for referring me to Scanner. I found the below code and I added the if statement and the length() statement. I wanted to know if a user just presses enter so I can let that user know that something must be entered. I think the string length let me know that.

Welcome again.

The Scanner object hasn't actually got a nextString() method; it uses next() instead. Beware of nextLine(); as explained here, there is a ris of nextLine() returning an empty String if used immediately after nextAnythingElse(). I haven't seen that problem well documented anywhere, but only on this website 

Sherman Willden wrote:Thank you for referring me to Scanner. I found the below code and I added the if statement and the length() statement. I wanted to know if a user just presses enter so I can let that user know that something must be entered. I think the string length let me know that.

Your if condition syntax is wrong I think. You should compare string values using .equals method like this if(string.equals(null)) or if(string.equals("")).

Campbell Ritchie wrote:The Scanner object hasn't actually got a nextString() method.

Sorry, that's what you get when you don't read the documentation all the way.

I think that this may be the better way to handle the if:
You can read about all the String class here https://docs.oracle.com/javase/9/docs/api/java/lang/String.html.
Here's my take on all this: if you want to be able to just press the <enter> key and get an empty String back, you have to use scanner.nextLine().  But as Campbell pointed out, this can lead to problems.  Here is some code to illustrate:
Here we're using scanner.next() to get Strings you run into a problem when you get to Address2: you can't just <enter> through the second address line if there's none.  You have to use scanner.nextLine() for that.

But if you change all the next() to nextLine(), you have a problem too:
If you run this program, you skip by the first address prompt. Why?  Because all the nextXxx() methods except for nextLine() leave the <enter> in the buffer (this is oversimplified, but...).  nextLine() sees that <enter> and believes you just pressed the <enter> key.  So the input is skipped.

So how do we fix this?  One way is to "consume" the <enter> in the buffer with nextLine(), so:
Another way is to only use nextLine() and do the convertion yourself, so:
And of course, you can go back to using next() for Strings and nextInt() for integers, but you have to accept that you can't have a blank field.  

This is why so many of us have written our own methods to do input from the keyboard (there's mine).
I think there is some confusion about the strlen variable. It is the length of the String, so it will be an int, which you can't call methods on, but you do have to use the == operator on.
The word strlen sounds like something out of C; maybe length would be easier to understand.
Thanks to all. I see that I have more to learn and I do appreciate this. I apologize for the short variable name. I started working with UNIX in 1985 and then it was about space and memory. I will self-document using longer names.


Sherman Willden wrote:. . . in 1985 . . . it was about space and memory. . . .

You paid considerably more for 1MB of RAM in 1985 than you would for 16GB nowadays. Even allowing for inflation.
This is where I am going with all of this plus learning java along the way.

The sqlPokerInsert project obtains the user's input for the postgresql venue_visits database visits table values. The program then verifies that the user's inputs are valid. Upon verification, the user's inputs are inserted into the venue_visits database visits table.

The user is queried for the input with a default answer submitted by the sqlPokerInsert program. The program obtains the default answer from the config.properties file. The program obtains the sql database acceptable values from the postgresql venue_visits database tables as listed below.
cities, games, hi_lo, kills, stakes, states, v_limits, and venues.

Upon the user's values being valid, the valid entries are inserted into the venue_visits database visits table.

Just for clarity below are the venue_visits fourteen entries.

I am forcing the date and time entries into an integer by checking that the date and time are in the following formats.

Time: 1000 or 1800 or 2400 or 0200
Date: 20180213 or 20180112 or 19890811

The poker visits information I have goes back to 1989. I actually played my first $1/$5 seven-stud game at Binion's in 1968. Two striper in the USAF and part-time dining hall work at Vandenberg and saved up a whoping $60 for the game. Lost it all that night in Las Vegas and had to return to Vandyland on what I had left. It was surprising what you could do on couple of bucks and a Trailways bus.
Since you are beginning in Java programming, I would first write this app with just a character interface.  I would write a class to deal with the prompting and inputting, or you can use one the classes developed by the people here.  Then have a class that holds the poker data.  Then write a class that uses the prompting and input class to get the data and set a poker data field.  Then write an SQL writing class that retrieves the poker data from the "get data" class and posts it to SQL.  You might also want to write a DAO (data access object) for the poker data.

Sounds a little complicated?  It is, but the nice thing about writing separate classes for different tasks is you can concentrate on one thing, like getting the data, then work on the others later.  The less each class "knows" about the others, the better.  Another advantage is if you want to add a GUI later, you can.
Thank you


This thread has been viewed 358 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 15, 2018 06:17:20.