简体   繁体   中英

How to print two different data type arrays?

Can anyone help? Choice 2 isn't working. It is suppose to display the employee ID when the user inputs the employee Name, but when the user enters the name nothing prints. The code has no errors.

public static void main(String[] args) {
int[] emplID={ 42577, 38611, 32051, 28627, 42061, 79451 };//employee ID
int ID = employeeID(emplID);
String[] emplNames= { "Bruce Wayne", "Barry Allen", "Hal Jordan", "Dinah Lance", "Oliver Queen", "Tineil Charles" };// Employee Names
search(emplNames, emplID);
//methods called from main


}

public static int employeeID(int [] emplID) {
    //check ID length
    for(int i=0; i< emplID.length; i++) {
        if((emplID[i] > 10000)&&(emplID[i] < 99999)) {
        System.out.print(emplID[i] + " - Valid ID length\n");

        }
        else {
            System.out.println(emplID[i] + " - Invalid ID! ID must be Five digits!\n");

        }//end of check length

        //check if ID is prime
         boolean isPrime = true;
            for (int j = 2; j < emplID[i]; j++) {
                if (emplID[i] % j == 0) {
                    System.out.println(emplID[i] + " - not prime");
                    isPrime = false;
                    break;
                } 
            }
            if(isPrime) System.out.println(emplID[i] + " - valid prime");//end of check prime
        }//end of employeeID method
    return 0;

}// end of ID checker

// search employee data public static void search(String[] emplNames, int[]emplID) {

        Scanner scan= new Scanner(System.in);
        //Menu Choice
        System.out.println("Please choose 1 to enter Employee ID or 2 to enter Employee Name:" );           

        int num = scan.nextInt();//input choice

        // Choice 1 to enter ID to display name
            if (num == 1) {
            System.out.println("Please enter Employee ID:");
            int searchID= scan.nextInt();
            for(int ID = 0; ID < emplID.length; ID++) {
            if (searchID == (emplID[ID])){
            System.out.println("Name: "+ emplNames[ID]);
        }
    }
            }
        // Choice 2 to enter name to display ID
            else if(num == 2) {
            System.out.println("Please enter Employee Name");
            String searchName= scan.next();
            for(int ID = 0; ID< emplID.length; ID++){
            if ((searchName.equals(emplNames[ID]))){
            System.out.println("ID: " + emplID[ID]);
            }
            }
            }   
            else 
                System.out.println("Employee Not Found");
}
}

I copied and pasted your code and ran it on my machine. Yes, choice 2 was not working for me either.

Before reading your code completely my gut feeling was that the cause of failure was in using the Scanner class to get the name of the employee. I have had similar issues in the past and the best move is to learn to use the InputStreamReader and BufferedStreamReader objects.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

1: I didn't do anything to your main()

public static void main(String[] args) {
    int[] emplID={ 42577, 38611, 32051, 28627, 42061, 79451 };//employee ID
    int ID = employeeID(emplID);
    String[] emplNames= { "Bruce Wayne", "Barry Allen", "Hal Jordan", "Dinah Lance", "Oliver Queen", "Tineil Charles" };// Employee Names
    search(emplNames, emplID);
}

2: I didn't do anything to your employeeID() function

public static int employeeID(int [] emplID) {
    //check ID length
    for(int i=0; i< emplID.length; i++) {
        if((emplID[i] > 10000)&&(emplID[i] < 99999)) {
            System.out.print(emplID[i] + " - Valid ID length\n");

        }
        else {
            System.out.println(emplID[i] + " - Invalid ID! ID must be Five digits!\n");

        }//end of check length

        //check if ID is prime
        boolean isPrime = true;
        for (int j = 2; j < emplID[i]; j++) {
            if (emplID[i] % j == 0) {
                System.out.println(emplID[i] + " - not prime");
                isPrime = false;
                break;
            }
        }
        if(isPrime) System.out.println(emplID[i] + " - valid prime");//end of check prime
    }//end of employeeID method
    return 0;

}// end of ID checker

3: It's in your search() method where I first created the InputStreamReader and the BufferedReader:

public static void search(String[] emplNames, int[]emplID) {
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader buff = new BufferedReader(in);

    //Menu Choice
    System.out.println("Please choose 1 to enter Employee ID or 2 to enter Employee Name:" );
    int num = 0;
    try {
        num = Integer.parseInt(buff.readLine());
    } catch (Exception e) {
        e.printStackTrace();
    }

4: Since choice 1 works fine, all I did was change your for loop to a for-each loop to make it easier to read.

    // Choice 1 to enter ID to display name
    if (num == 1) {
        System.out.println("Please enter Employee ID:");
        int searchID = 0;
        try {
            searchID = buff.read();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i : emplID) {
            if (searchID == i) {
                System.out.println("Name: " + emplNames[i]);
            }
        }

5: Here is what I did to make your 2nd Option work. Again, get the String from user via BufferedReader object's readLine() method. Then, it was just letting your for-loop searching for a match. That's it. Afterward, I ran the program and tested it for all the names you had above, works fine.

    } else if (num == 2) {
        System.out.println("Please enter Employee Name");
        String searchName = "";
        try {
            searchName = buff.readLine();
        } catch(Exception e) {
            e.printStackTrace();
        }

        for(int ID = 0; ID< emplID.length; ID++){
            if ((searchName.equals(emplNames[ID]))){
                System.out.println("ID: " + emplID[ID]);
            }
        }
    } else {
        System.out.println("Employee Not Found");
    }
  }
}

6: Yeah, Scanner has an issue where it either doesn't read the entire line or you need to flush the stream before getting the input. It caused a lot of problems for me in a bunch of easy programs. Then I switched to using the InputStreamReader and BufferedStreamReader combo. Just wrap them in try-catch blocks, and you're fine. Look into it, it will the behavior of your code and your life a lot easier.

7: I hope this was helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM