Karthickyuvan
Karthickyuvan

Reputation: 428

java String Array : getting information from the user and display the number in the array

I was Created a String Array of names and Int array for the Phone Number and asking the user to select the names from the list of array and we want to display the exact number of the name

public class main {

public static void main(String[] args) {
    String[] names = {"Name one  ","Name two ","name three ","Four"};
    int[] num ={12345,56789,25622,21478};
    for (int i = 0;i < names.length;i++)
    {
        System.out.println(names[i]);

    }
    System.out.println("Enter the Name To get Numbers");
    Scanner scanner = new Scanner(System.in);
    String name=scanner.next();
    for (int i=0; i <names.length; i++)
    {
        if (name.equals(names[i]));
        {
            System.out.println(num[i]);
        }

    }
    

}

the output what i get was

Name one  
Name two 
name three 
Four
Enter the Name To get Numbers
Four
12345
56789
25622
21478

Process finished with exit code 0

what ever name i enter it displays all the numbers in the array how to solve this ? The Output what i Need was

Name one  
    Name two 
    name three 
    Four
    Enter the Name To get Numbers
    Four
    21478

Upvotes: 1

Views: 55

Answers (1)

Thiyagu
Thiyagu

Reputation: 17900

You have a semicolon at the end of the if condition. Remove that and your code will work fine.

if (name.equals(names[i])) {
        System.out.println(num[i]);
}

Upvotes: 3

Related Questions