Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me how I can able to get the string value from the user? Below is my code:

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"); 

     }

     }

    }

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

Basically, in able to check for the equality of the string, you should use the equals method rather than "=="

if(i.equals("Ronald"))

The Output which you will get:

Whose information do you want?[Ronald, Donald]

Ronald

Ronald is CEO and is in Room No 20

I hope this will help.

Want to become a Java Expert? join Java Training now!!

Want to know more about Java? Watch this video on Learn java | Java Programming for Beginners:

Related questions

0 votes
0 answers
0 votes
1 answer
asked Apr 1, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
asked Mar 8, 2021 in Java by dante07 (13.1k points)

Browse Categories

...