Results 1 to 7 of 7
  1. #1
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Question what's wrong here ?

    hi,

    i m just trying to find a name from Array by using scanner..
    no error here, but not showing although its there...
    Java Code:
    String[] friendList={"chanan","tapan","Amar","santosh","deepak"};
    
      String name;
      //String name="Amar";
    
      Scanner scan=new Scanner(System.in);
      System.out.print("Type your Friend's Name: ");
      name=scan.next();
      
        for(int i=0;i<5;i++){
        System.out.println(friendList[i]);
     
      if(friendList[i]==name){
      System.out.println("Found in List");}
      
      else { System.out.println("Not Found in List"); }
    the output is:
    Type your Friend's Name: Amar
    chanan
    Not Found in List
    tapan
    Not Found in List
    Amar
    Not Found in List

    santosh
    Not Found in List
    why its not working ?
    if i do like this, String name="Amar";
    then its working correctly.

    Pls anybody tell me, where is the wrong ?
    what we are thinking, it might not be true

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    797
    Rep Power
    6

    Default

    u just check with name.trim()
    Ramya:cool:

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Java Code:
    if( (friendList[i]).equalsIgnoreCase(name)) {
    This is way it's not work

  4. #4
    doha786 is offline Member
    Join Date
    Mar 2009
    Posts
    48
    Rep Power
    0

    Default

    thanks...............

    its working...
    what we are thinking, it might not be true

  5. #5
    javastuden's Avatar
    javastuden is offline Senior Member
    Join Date
    Nov 2009
    Posts
    160
    Rep Power
    5

    Default

    hi,
    if the problem is like this then what will the output
    String freind="Amar";
    String name="Amar";
    if(freind==name)
    System.out.println("Found");
    else { System.out.println("Not Found in List"); }

    ok
    then if we are using the
    Scanner scan=new Scanner(System.in);
    System.out.print("Type your Friend's Name: ");
    {name=scan.next();}
    and giving the name from cmd then what will be the out put
    because the scan.next() return type is String and Object so how the jre(is this correct means who will interpret the condition is correct is that jre or jdk) will identify that is onject or string that user mentioned

    by which pgm the complier in java made up of and by which pgm the interperter in java is made up of

    i heard that compler is by java and interpretor by c++ then where the c++ is complied
    where is c++ is used why c++ used if c++ used then mutiple inhertence is not used as in c++

    what is the need of interpreter as the java by jdk is compliling to form class then where the is interpertor sitting

    please tell me

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,746
    Rep Power
    19

    Default

    Quote Originally Posted by javastuden View Post
    hi,
    if the problem is like this then what will the output
    String freind="Amar";
    String name="Amar";
    if(freind==name)
    System.out.println("Found");
    else { System.out.println("Not Found in List"); }
    This will print "found", but only because the compiler turns both "Amar" strings into a single object so the use of "==" works. If you did "new String("Amar")" for each of those (or just one) it wouldn't work. You'd get "Not Found".

    Quote Originally Posted by javastuden View Post
    ok
    then if we are using the
    Scanner scan=new Scanner(System.in);
    System.out.print("Type your Friend's Name: ");
    {name=scan.next();}
    and giving the name from cmd then what will be the out put
    because the scan.next() return type is String and Object so how the jre(is this correct means who will interpret the condition is correct is that jre or jdk) will identify that is onject or string that user mentioned

    by which pgm the complier in java made up of and by which pgm the interperter in java is made up of

    i heard that compler is by java and interpretor by c++ then where the c++ is complied
    where is c++ is used why c++ used if c++ used then mutiple inhertence is not used as in c++

    what is the need of interpreter as the java by jdk is compliling to form class then where the is interpertor sitting

    please tell me
    The output will always be false, since you cannot use "==" to compare objects...unless you want to know if they are the same object (that is the same reference). That's what equals() is for.

    Not too sure what the rest of this is going on about though.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by doha786 View Post
    thanks...............

    its working...
    Cool, so can you mark the thread solved if you've found the complete solution to your question. :)

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. where did i go wrong?
    By Sri Vidhya in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:11 PM
  3. Please help, what am I doing wrong?
    By mmac1218 in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 11:21 PM
  4. what is wrong?
    By pinguxxx in forum Advanced Java
    Replies: 3
    Last Post: 07-15-2009, 12:33 PM
  5. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 01:07 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
  •