English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

should my while loop be (aX != "") o r (aX != null)?

2006-09-10 09:47:02 · 4 answers · asked by cantgybe 1 in Computers & Internet Programming & Design

4 answers

The answer depends on the form of input.

If the input is from a JTextField, for example, then if you mean that the user has not entered anything and hits Enter or clicks a button or some other action, then the text field's getString() method will return a null. Be aware that this is NOT true if they enter one or more spaces. You should therefore call getString(), check for null, and if not null, call trim() on the resulting String and then check for length of 0 (i.e. it was all blank spaces and trim() cut it down to "").

If the input is from the console (e.g. System.in) and you have wrapped it with a BufferedReader and are calling readLine(), then hitting Enter without entering anything will return an empty String. You can trim() the result and check for length of 0 to decide if it is time to terminate.

2006-09-10 21:03:17 · answer #1 · answered by vincentgl 5 · 0 0

while dont you just test it out?

I presume you mean when the users presses a key - as an empty string would imply the user hasn't presssed anything.

have used a lot of jave, but have used previous languages.

the 2 lines you have put above equate to the following
ax!="" - ax not(!=not) equal to a null value (i.e. nothing been pressed)

ax!=null =ax not equal to a null value (empty value)

both commands are pretty much the same.

other ways I have seen this done in other languages include the following

(ax -ne "") = ax not equal to empty string

also usually use while, wend loops, do until loops, etc.

Please also make sure you are talking about java and not javascript as these are totally different entities

2006-09-10 11:33:04 · answer #2 · answered by Anonymous · 0 0

aX.length() == 0

are you really using java? looks like C

2006-09-10 09:57:14 · answer #3 · answered by Anonymous · 0 0

while(aX.length() != 0)

2006-09-10 10:06:47 · answer #4 · answered by ? 2 · 0 0

fedest.com, questions and answers