wood burning stoves*
The moose likes Beginning Java and the fly likes while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop" Watch "while loop" New topic
Author

while loop

umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
Can anyboby see any mistake with this code?

while((name==" ") || (course==" "))
{
name=JOptionPane.showInputDialog("Please enter student name "+ (k+1));
course=JOptionPane.showInputDialog("Please enter student course "+ (k+1));
}

Its not giving me any error message but it doesnt do any control at all.
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
are you sure that those two strings are initially ""

or are they perhaps initially null? (I'm betting on the second, but I can't say for sure without seeing the rest of your code).


Try this:

BEFORE you enter the loop, do this code:



That should tell you why you're having troubles...

- Adam

[ January 12, 2007: Message edited by: Adam Nace ]
[ January 12, 2007: Message edited by: Adam Nace ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
You probably want:

while (name.equals(" ") || course.equals(" "))

or if name or course could be null it would be better to do:

while (" ".equals(name) || " ".equals(course))


While I'm here let me mention two things:

1) There is a difference between " " (the String consisting of one space) and "" (the empty String). Your code is comparing against the former. Is that what you intend?

2) At the risk of confusing things you could also write
while ((name.intern() == " ") || (course.intern() == " "))
This would work (unless name or course could be null) but I do not recommend it. I probably shouldn't even have mentioned it in a beginner forum.)
[ January 12, 2007: Message edited by: Brian Cole ]

bitguru blog
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

what happens if the user enters TWO spaces?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Brian Cole:


2) At the risk of confusing things you could also write
while((name.intern() == " ") || (course.intern() == " ")).
This would work (unless name or course could be null) I wouldn't recommend it. I probably shouldn't even have mentioned it in a beginner forum.)


Okay, I'm going to go beyond "not recommending it" and say "this is a ridiculous idea!"

Yes, it would work BUT, interning a string right before you are going to test it for equality, then you gain absolutely NOTHING, because it takes more time to intern the string then it does to simply compare it to another string directly. Interning only helps if
1) You are going to test it against another interned string or string literal for equality many times; or
2) You perform the interning at a time when the system is idle, and therefore has the spare time to do the intern, and then saves time later when it does the check during a time critical operation.

Beyond that, interning saves no time.

- Adam
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Fred Rosenberger:
what happens if the user enters TWO spaces?


That's a very good point! Instead of specifying what a bad value looks like, perhaps you should specify what a GOOD value should look like!

- Adam
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
if (name == ""){ System.out.println("String is EMPTY");} else if (name == null){ System.out.println("String is NULL");} else { System.out.println("String is not empty or null initialized");}


What is the difference between EMPTY and NULL. Till now i was thinking they both means a string without any data in it, i didnt know it makes any difference.
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
Brian Cole
for(int k=0;k<size;k++)
{
name=" ";
course=" ";
while((name.equals(" ")) && (course.equals(" ")))
{
name=JOptionPane.showInputDialog("Please enter student name "+ (k+1));
course=JOptionPane.showInputDialog("Please enter student course "+ (k+1));
}

Student obj=new Student(name,course);
strObj.AddToArray(obj);

}


I ve done how you said, its working but its just checking the name not the course.
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by umut uzumcu:


What is the difference between EMPTY and NULL. Till now i was thinking they both means a string without any data in it, i didnt know it makes any difference.


Not exactly.

In java. ALL object type variables are stored as references -> the variable does not actually contain the data of the object, it contains a reference to an object with data in it. If that reference does not point to an object at all, it is said to be NULL (i.e. the object does not exist). This is the initial state of any object type variable in java, until you execute code to initialize it.

On the other hand, and EMPTY string is a String object that DOES exist, but the exact value of the string is that it has NO characters in it at all.

For instance, the code

String s;

Does not actually create a String object. It's value is NULL. But the code:

String s = "";

or

String s = new String("");

DOES create a String object, having a value with no characters.

- Adam
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122

The reference variable name of type String refers to a String object which "has no characters" as content... The String is "empty".


The reference variable name of type String refers to nothing. There is no String object name is referring.
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by umut uzumcu:
Brian Cole


I ve done how you said, its working but its just checking the name not the course.



You've changed the || to &&, which means that if EITHER one of the values is supplied, then it will not do another pass through the loop.

On the other hand, if you had used ||, if one of the values is NOT supplied, it would ask you to resupply BOTH.

What you probably want to do is put name and course into separate loops, so that it will first make sure that you have supplied a valid name, THEN it will make sure that you have supplied a valid course,.


- Adam
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43


This code is working, But there is another problem, doesnt matter what name and course i enter, on the output always the first name and course is printing "null".
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by umut uzumcu:


This code is working, But there is another problem, doesnt matter what name and course i enter, on the output always the first name and course is printing "null".


Hmm... everything there looks okay. I would check for bugs in either the Student class, OR in the code where you print out the values.

Since the code used for the name and course are essentially identical, they should both have the same effect.

- Adam Nace
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop
 
Similar Threads
while loop question
Compile error
Control for String
problem with hashCode()
Array lengths