0

I am new to java and practicing java programs. My code is as below:

String i;
    String[] people={"Ronald","Donald"};

System.out.println("Whose information do you want?"+Arrays.toString(people));
Scanner s =new Scanner(System.in);
**i=s.nextLine();**

          if(i=="Ronald")
     {
         System.out.println("Ronald is CEO and is in Room No 20"); 
     }
          if(i=="Donald")
     {
         System.out.println("Donald is postman and is in Room No 10"); 
     }

     }
    }

This is actually a simple program. What I wanted to know is whenever user inputs Ronald, its print statement should be printed in console. But the user should be asked whose information it wants to see. I wanted to know how to take string values from user.

Thank you in advance :)

2 Answers2

1

Use the equals method when comparing Strings in java

if ( i.equals("Ronald") ){

}
copeg
  • 8,290
  • 19
  • 28
  • As I may add, the i == "Ronald" tests if i and "Ronald' are in the same memory place. To compare the content of two objects, you must use the .equals operator. – JFPicard Mar 27 '15 at 19:30
  • i want to know how to declare the string array i have highlighted that takes input from the user taking scanner. for eg. if i was an int i could have wriiten s.nextInt(); in the same way what are the procedures for string values with array – JavaBeginner Mar 27 '15 at 19:33
0

check string equality this way

  if(i.equals("Ronald"))

not using ==

OUTPUT

Whose information do you want?[Ronald, Donald]
Ronald
Ronald is CEO and is in Room No 20

Use this class it fully works:

    import java.util.Arrays;
import java.util.Scanner;
class ronald {
    public static void main(String[] args) {
     String i;
     String[] people={"Ronald","Donald"};
     System.out.println("Whose information do you want?"+Arrays.toString(people));
     Scanner s =new Scanner(System.in);
     i=s.nextLine();
         if(i.equals("Ronald")) {
         System.out.println("Ronald is CEO and is in Room No 20"); 
         }
         if(i.equals("Donald")) {
         System.out.println("Donald is postman and is in Room No 10"); 
         }
     }
    }

output for Donald

Whose information do you want?[Ronald, Donald]
Donald
Donald is postman and is in Room No 10
adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • thank you. i got that error. but my question is: what to write when I want the user to enter "Ronald" – JavaBeginner Mar 27 '15 at 19:33
  • your question is ... ??? – adrCoder Mar 27 '15 at 19:34
  • eg: scanner s=new scanner(system.in); s.nextInt(); is used for int. I have an array of string and i want the user to enter the string value from the array. So what should I do for that? – JavaBeginner Mar 27 '15 at 19:37
  • ok see my edit it fully works. i=s.nextLine(); will get the next line in a string which is what you want ! – adrCoder Mar 27 '15 at 19:38
  • i had already edited .equals. this code still does not work. thank you . will try and work it some other way. :) – JavaBeginner Mar 27 '15 at 19:44
  • the code works I tried it in my computer... I dont know what the problem is. When you realize it works please accept my answer http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – adrCoder Mar 27 '15 at 19:45