This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why does this 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 » Java in General
Bookmark "Why does this"if" statement get skipped?" Watch "Why does this"if" statement get skipped?" New topic
Author

Why does this"if" statement get skipped?

Jamie
Greenhorn

Joined: Jan 15, 2000
Posts: 10
After a couple of hours of trying to figure out why the program below skips over the "if" statemnent in line 107,"if (searchFirstname == null)", and takes the "else" branch whenever no first name is entered, I remain mystified. I imagine it's something very simple, but I'd sure appreciate someone spotting for me.
import java.io.*;
class PhoneEntry
{
String lastname; // name of a person
String firstname;
String phone; // their phone number
PhoneEntry( String ln, String fn, String p )
{
lastname = ln.toUpperCase(); firstname = fn.toUpperCase(); phone = p;
}
}
class PhoneBook
{
PhoneEntry[] phoneBook;
PhoneBook() // constructor
{
phoneBook = new PhoneEntry[ 9 ] ;
phoneBook[0] = new PhoneEntry( "Barclay", "James", "(418) 665-1223" );
phoneBook[1] = new PhoneEntry( "Dunbar", "Grace", "(860) 399-3044" );
phoneBook[2] = new PhoneEntry( "Kratides", "Paul", "(815) 439-9271" );
phoneBook[3] = new PhoneEntry( "Smith", "Violet", "(312) 223-1937" );
phoneBook[4] = new PhoneEntry( "Wood", "John", "(913) 883-2874" );
phoneBook[5] = new PhoneEntry( "Barclay", "Bill", "(418) 555-1223" );
phoneBook[6] = new PhoneEntry( "Barclay", "Ted", "(418) 115-1223" );
phoneBook[7] = new PhoneEntry( "Smith", "Sarah", "(312) 555-1937" );
phoneBook[8] = new PhoneEntry( "Judd", "James", "(418) 665-1223" );

}
PhoneEntry flsearch( String targetLastName, String targetFirstName)
{
for ( int j=0; j < phoneBook.length; j++ )
{
if ( phoneBook[ j ].lastname.equals( targetLastName ) && phoneBook[ j ].firstname.equals( targetFirstName ))
return phoneBook[ j ];
}
return null;
}

PhoneEntry lsearch( String targetLastName)
{
for ( int j=0; j < phoneBook.length; j++ )
{
if ( phoneBook[ j ].lastname.equals( targetLastName ) )
return phoneBook[ j ];
}
return null;
}
void print( String targetLastName)
{
for (int j=0; j < phoneBook.length; j++)
{
if ( phoneBook[ j ].lastname.equals( targetLastName ) )
{
System.out.print( phoneBook[j].lastname + " " +phoneBook[j].firstname + " " +phoneBook[j].phone); //name and phone are the
//strings you want to print
System.out.println( );
}
}
}
}
class pb3
{
public static void main ( String[] args ) throws IOException
{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));

PhoneBook pb = new PhoneBook();
String inData;
String searchLastname = "Wokloy";
String searchFirstname = "Wokloy";

boolean cont;
cont = true;
while (cont)
{
//ask for input
System.out.println("Enter last name");
inData = bf.readLine();
searchLastname = inData.toUpperCase();

if (searchLastname.equals("QUIT"))
{
System.out.println("Goodbye" );
cont = false;
}
else
{
// search for searchname
PhoneEntry entry;
System.out.println("Enter first name");
inData = bf.readLine();
searchFirstname = inData.toUpperCase();


if (searchFirstname == null)
{
pb.print(searchLastname);
}
else
{
entry = pb.flsearch( searchLastname, searchFirstname );
if ( entry == null )
System.out.println("Name not found. Try another." );
else
System.out.println( entry.lastname +" " +entry.firstname + ": " + entry.phone );
}
System.out.println("This is what should print with no first name entered:");
pb.print(searchLastname);
}

}
}
}
Thandapani Saravanan
Ranch Hand

Joined: Oct 17, 1999
Posts: 117
When i tried executing this program i found that the line
inData = bf.readLine()
doesn't return null when i press just the Enter key. It (inData) has a zero length String. Could anybody tell what is happening?
Thanks.


Saravanan
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20496
    ∞

Because inData is never null. If inData were null, you would get a null pointer exception on the line that says "searchFirstname = inData.toUpperCase();"
I've never tinkered with the terminal input stuff in Java, but I'm not suprised that just pressing enter returns a string with zero length. Maybe that's what should be tested for: if ( searchFirstName.length() == 0 )


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jamie
Greenhorn

Joined: Jan 15, 2000
Posts: 10
Thank you, Thandapani and Paul, for your comments. I solved the problem by using inData.equals(null) right after the readLine(), as shown below.
I also discovered that my free version (Community Edition) of Java WorkShop 3.0.6 from Sun is somewhat buggy. Has it been that way for others, or did I just get a slightly imperfect download? Can you recommend a better free editor?

PhoneEntry entry;
System.out.println("Enter first name");
inData = bf.readLine();

if (inData.equals(null))
{
searchFirstname = inData.toUpperCase();
entry = pb.flsearch( searchLastname, searchFirstname );
if ( entry == null )
System.out.println("Name not found. Try anew." );
else
System.out.println( entry.lastname +" " +entry.firstname + ": " + entry.phone );
}
else
pb.print(searchLastname);

paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20496
    ∞

Free editor, eh?
I know there is one that is written entirely in Java, although I forget what it is called. Maybe jedit. If you go to the links page, there should be something there about cafe au lait - I think they had some reviewish stuff about it.
If I were going to get on a free editor bandwagon now, I think I would break down and really learn vi. Everyone agrees that once you know, you can get more done with it than anything else. The trick is that the editor is a nightmare to learn. The reason I would choose vi instead of emacs or some of the others is that vi is on every unix box already. Granted, I do most of my editing on NT, but once in a while I need to change something on a unix box and vi is the only editor.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Paul wrote: Everyone agrees ...
I'm disappointed, Paul. The last thing we need here is an editor holy war.
Really, you have to find out a lot more about how someone works, and how they would prefer to work, before recommending an editor. It can be very irritating to learn a powerful editor and its keystrokes, and then find that a of of the time you can't use it. This is compunded when the editors have entirely different operating assumptions. Some people (myself included) just don't like modal editors (and that includes emacs to a large degree).
Although vi is present on almost all unix systems, it can't always be used. If you have the wrong terminal type set, it can be awful (and so can emacs etc.); and there are many telnet programs/terminal emulators/window systems which can't seem to get this right.
The unix philosophy is really to hava available as many tools as possible, and be conversant with the use of all of them. What do you do if you can't run any visual editors? Well there's still ed, sed, awk, perl, echo ...
Learn ed and you really can't go wrong. ed will work quite happily on a 110 baud teletype.
Or how about edit using the system and editor of your choice, and transfer files by nfs/samba/ftp etc.
Sorry for the rant, but I'd be very wary of introducing an editor war here. (http://c2.com/cgi/wiki?EmacsVsVi)


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20496
    ∞

Are you saying, Frank, that once you thoroughly learn vi (and you have vi running) that there are people who would not be as proficient with it? I used vi for a few months about ten years ago. And I used it for about two minutes a month ago. I hate it! But I can see the truth in what the vi gurus say. Once you've learned it, you can really hum on it. All your fingers are on the home row.
I use Visual SlickEdit now. It has some nice features and some nice bugs too. I've programmed it to emulate WordStar/TurboPascal2/MicroStar which I've gotten used to over the years. Packing this thing from job to job has been a small hassle of its own. I'm thinking it may be time to teach this old dog a new trick. But which trick?
Not vi?
Jamie
Greenhorn

Joined: Jan 15, 2000
Posts: 10
Thanks, Paul and Frank, for your comments on editors. I did find some reviews of IDEs at cafe au lait, and their conclusion was that all of them are still too buggy to be worthwhile. They recommend just a simple editor like (but not) Notepad, plus the JDK.
Jamie
Greenhorn

Joined: Jan 15, 2000
Posts: 10
It can be scary to realize how much your mental powers vary from day to day. The solution I thought I had found above "worked" because I got it backwards, and thus when the if branch failed, I was getting the else branch that should have been the if.
The real solution was your suggestion, Paul, using length(). Thank you.
 
wood burning stoves
 
subject: Why does this"if" statement get skipped?
 
Similar Threads
Please Help me debug program
Please Help Me with Program
Are my tools buggy or are these programs significantly different?
Question for Frank Carver
Casting Problem *Help Please