Results 1 to 4 of 4
  1. #1
    veloDMC is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default Need help with an illogical logical statement

    I need some help to understand why a logical statement is not working as I expect it should.
    I am very new to Java and am creating a simple program that uses a client and server class. The code below is from the server class.

    The client class asks the user to input a studentís name and test scores. The server class validates the user input, amongst other things.

    To test the validateData() method, I input an empty string as a studentís name when the client class asked me to. (ie.simply hit the enter key).

    However the logical 'while' statement below never evaluates as true and always displays the negative condition action (ie ďThis is the else statementĒ). I have also tried using an if-else configuration but the same thing happens.

    I tried reversing the problem and using while (name != "") and entering a name eg Bob. In this case the statement will evaluate as true and return the positive condition action and print ďThe studentís name has not been entered.Ē

    This problem is tying me up in knots and I donít know how to resolve it. I appreciate your help.
    Java Code:
    public class Student {
    	// Manage a student's name and three test scores.
    	
    	//Instance variables
    	//Each student object has a name and three test scores
    	private String name;	//Student name
    	private int test1;		//score on test 1
    	private int test2;		//score on test 2
    	private int test3;		//score on test 3
    
    public void validateData(){
    		//validate student data
    		
    while (name == ""){
    //this line is not working and I don't know why.
    //I think 'name' has a value even if it is "" and so always 
    //returns as a false value and triggers the else statement
    		System.out.println("The student's name has not been entered.");
    		}
    		System.out.println("This is the else statement.");
    //the else statement is displaying even if i put an empty string as a name
    {
    {
    Last edited by pbrockway2; 08-31-2011 at 02:02 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Hi, welcome to the forums.

    It helps everyone if you use code tags when posting code: put [code] at the start of the code and [/code] at the end. That way the code will be readable when it appears here.

    -----

    Use the String method equals() to compare strings. == tests for identity whereas you are merely trying to see if two strings are made up of the same sequence of characters which is what equals() tests for.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    Java Code:
    while (name == ""){
    Don't use the == operator when comparing the contents of String objects. Use the equals method or for this test you could use the length method.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Is it too late to mention DOUBLE POSTED over at the Ranch?

Similar Threads

  1. if block with logical or
    By Ranu in forum New To Java
    Replies: 6
    Last Post: 07-01-2010, 09:11 AM
  2. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 09:12 AM
  3. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 02:11 AM
  4. Listing Logical Drives
    By Juggler in forum New To Java
    Replies: 3
    Last Post: 08-10-2008, 08:08 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •