Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: problems with equals method.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    India
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems with equals method.

    When i use the following code, the equals mehod return true if i use numbers , if i use a string as given in the code the method always returns false what could be the reason. The store.txt file contains the following lines as given. If i give the equal string as 1000 the match is produced and if i give as kp or kkn the output always states no match .


    store.txt
    kp
    1000
    kkn
    500

    [code=java]
    import java.io.*;
    class home
    {
    public static void main(String args[])
    {
    try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("store.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    int value=0;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)
    {
    // Print the content on the console

    System.out.println (strLine);
    if(strLine.equals("kp"))
    {
    System.out.println("Match found");

    value = Integer.parseInt(strLine);
    value=value+100;
    System.out.println(value);
    }
    else
    System.out.println("No match");
    }
    //Close the input stream
    in.close();
    }
    catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }



    }
    }
    [/code=java]

    Plz help me out


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: problems with equals method.

    Add a println statement just before the equals statement and print out the String being compared in the equals method so you can see what the computer is seeing. Be sure to add delimiters on both sides fo the String:
    (">" + theString +"<" ):
    That should explain what is happening.

    Post the console contents from when you execute the code:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Check you code tags. Use the #icon above the input box
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: problems with equals method.

    Try using a
    strLine = (br.readLine().trim())
    The trim method as part of the String library will remove any whitespace characters on the line you read into the buffer. Hope this helps
    Last edited by hackthisred; April 29th, 2012 at 11:15 PM.
    f34r th3 kut3 1z

Similar Threads

  1. equals() method.
    By TP-Oreilly in forum Object Oriented Programming
    Replies: 2
    Last Post: February 11th, 2012, 11:59 AM
  2. Using the .equals method
    By TenaciousE in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 05:24 PM
  3. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  4. Acessor Method Problems for Projectile Motion
    By Kaiki in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 28th, 2011, 08:01 PM
  5. Equals Method
    By Sninald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2010, 03:06 AM