Results 1 to 8 of 8
  1. #1
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    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

    Java Code:
    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());
      }
    
      
      
      }
    }
    Plz help me out
    Last edited by JosAH; 04-28-2012 at 10:14 AM.

  2. #2
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: problems with equals method.

    Try using

    Java Code:
    if (r.compareToIgnoreCase("kp")==0)
    also if you're reading from a file I recommend making sure there's no rubbish around it by doing

    Java Code:
    if (r.trim().compareToIgnoreCase("kp")==0)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,043
    Blog Entries
    7
    Rep Power
    23

    Default Re: problems with equals method.

    If your String variable strLine equals "kp" it definitely doesn't represent an int value.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  4. #4
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: problems with equals method.

    This is true. I hadn't noticed you're looking for a string then trying to convert it to an int but it should spurt out "Match Found" before crashing :P

  5. #5
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: problems with equals method.

    Can someone clearly explain whats the problem with that if statement given in my code. ?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: problems with equals method.

    What I read your code as trying to do is:

    "If String is "kp" try to parse the String as an int."

    As Jos points out, this doesn't make sense -- how can one parse "kp" as an int?

  7. #7
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: problems with equals method.

    Ah! i dint paste the code on the line 24.
    Leave the lines 24 25 26 27 and execute the code still the same problem occurs man. !

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    27

    Default Re: problems with equals method.

    Quote Originally Posted by mkarthik90 View Post
    Ah! i dint paste the code on the line 24.
    Leave the lines 24 25 26 27 and execute the code still the same problem occurs man. !
    Please paste the current code you're testing. Let's remove all doubt, OK?

Similar Threads

  1. What is the use of equals () method?
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 08-04-2011, 05:57 PM
  2. equals method :::::HELP:::::
    By alihht in forum New To Java
    Replies: 5
    Last Post: 03-09-2010, 08:19 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 10:26 PM
  5. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM

Posting Permissions

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