Results 1 to 2 of 2
  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default comapring a '-' charcter

    Hi,

    In my program I am reading some data of type String,int. Some values are empty. There I am entering - character. This "-" character indicates that the field is empty. Here I have to compare this current read data with previous data. If I found "-" in any one of the operator where I am comparing I don't update otherwise there is a need of updation.

    Here I am getting the problem.

    I wrote the code like this:

    String s2=author[k].getDept(); // reading the input.
    String t2=author[l].getDept(); // this input field may contain "-" as a input string
    System.out.println("Dept: "+k+"is "+s2+"Dept: "+l+"is "+t2); // for verification purpose
    if(s2=="-" || t2=="-")
    System.out.println("Don't update");
    else
    {
    if(s2!="-" && t2!="-")
    {
    System.out.println("Crossed if condition");
    if(s2.equalsIgnoreCase(t2))
    {
    System.out.println("updation");
    weightBetweenAuthors[k][l] += 1;
    weightBetweenAuthors[l][k] += 1;
    System.out.println("wg b/w au["+k+"]["+l+"]: "+weightBetweenAuthors[k][l]+ "["+l+"
    ]["+ k+"]: "+weightBetweenAuthors[l][k]); // just printing updated values
    }
    }
    }

    Here what happens is even the string is "-" it is not entering the if stmt and executing the else part and printing
    Crossed if condition
    updation

    and printing updated value.

    In this part the mistake is. But what is the mistake I am not getting. The input is of type String thats why in comparison I kept that - character in double quotes.

    please respond fast.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Comparing strings

    You can't compare a string with "==", you have to use the string equals() method...
    Java Code:
    if(Barney.equals("Fred"))
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

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