-1

I have to write a program which keeps information of 10 employees(Employee Number, Surname, First Name, Position, Salary, Percentage Attendance, and Number of Casual Leaves, and Number of Sick Leaves remaining) in a single string (employeeDetailsStrings) separated by a # that is in the format

E101#John#Wills#Software Engineer#40000#78#25#12.

The system should allow the user to search for an employee based on either his Surname or Employee Number, and then display all the details corresponding to that employee. The system should give and error message in case the employee is not found. The program uses an array to hold the details of the 10 employees.

Here is what I have come up so far:

import java.util.*;

public class labsheets {

        public static void main(String args[]){


            Scanner sc= new Scanner(System.in);

            String[] empDetString= new String[10];
            String input;
            int i;

            System.out.println("Enter emp details: ");

            for (i=0; i<empDetString.length; i++){

                empDetString[i]= sc.nextLine();
                empDetString[i].split("#");

                }

            }

I don't know how to split each of these 10 arrays of employee details separated by a hashtag as apparently my approach is wrong. The output should be as follows after a match has been found through either the employee number or surname.

Output:

Employee Number is : E101 
Surname : Wills
First Name : John
Position : Software Engineer
Salary : 40000
% Attendance : 78
Casual Leaves left : 25 
Sick Leaves left : 12
mr.celo
  • 585
  • 1
  • 5
  • 17
Tia
  • 1,220
  • 5
  • 22
  • 47
  • see : http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java - empDetString[i].split("#") does nothing, test your indexes (+1, +2, ...) or you'll get exception, etc. – guillaume girod-vitouchkina Nov 27 '15 at 12:54
  • `for(i=0;i – Tom Nov 27 '15 at 12:55
  • 1
    Btw: what do you think happens here `empDetString[i].split("#")`? Hint: it doesn't do what you might think it does ... – Tom Nov 27 '15 at 13:04
  • @Tom I thought that my approach to splitting the array is not correct. I'll try to correct it. Thanks – Tia Nov 27 '15 at 13:10
  • @Tom I actually have to store the details of 10 employees(in an array) and then split each of the 10 arrays to access each part for the query and displaying of the details. I don't know how to split the 10 arrays and access them later on – Tia Nov 27 '15 at 13:15

2 Answers2

1

Because you have this array with size 2.

String[] empDetString = new String[2];

You should add a size more than 2, because the array doesn't auto increment. For that when you try to do this.

System.out.println("First name: " + empDetString[i + 2]);

You have java.lang.ArrayIndexOutOfBoundsException.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • I actually have to store the details of 10 employees(in an array) and then split each of the 10 arrays to access each part for the query and displaying of the details. I don't know how to split the 10 arrays and access them later on because my approach of splitting the arrays is apparently wrong – Tia Nov 27 '15 at 13:19
1

You are accessing items outside of you range, like in this line

empDetString[i+1]

which is encapsulated inside this loop

for(i=0;i<empDetString.length;i++){

Inevitably, your i will be more than the size of the array.

To correct this, since your last call is empDetString[i+7], make sure to limit your for loop, like so:

for(i = 0; i < empDetString.length - 7; i++){

Edit:

Also, there are no curly brackets in your if statement. Only the first line after that would be encapsulated in the condition.

Finally, you are not assigning a return value for the split(). Try something like:

String[][] empDetString= new String[2][10];
String input;
int i;

System.out.println("Enter emp details: ");

for (i=0; i<empDetString.length; i++){
  empDetString[i]=sc.nextLine().split("#");
}

Then later you can access as:

for(i=0;i<empDetString.length;i++){

    if(input.equals(empDetString[i][0])) {
        System.out.println("Employee number:"+ empDetString[i][0]);
        System.out.println("Surname: "+ empDetString[i][1]);
        System.out.println("First name: "+empDetString[i][2]);
        System.out.println("Position: "+empDetString[i][3]);
        System.out.println("Salary: "+empDetString[i][4]);
        System.out.println("% Attendance: "+empDetString[i][5]);
        System.out.println("Casual Leaves Left: "+empDetString[i][6]);
        System.out.println("Sick leaves left: "+empDetString[i][7]);
        break;
    }
}
mr.celo
  • 585
  • 1
  • 5
  • 17
  • Thanks, I got that. But I actually have to store the details of 10 employees(in an array) and then split each of the 10 arrays to access each part for the query and displaying of the details. I don't know how to split the 10 arrays and access them later on because my approach of splitting the arrays is apparently wrong. – Tia Nov 27 '15 at 13:21
  • Thank you so much. I had a vague idea that it should be done this way but your answer put everything into perspective and set it right. Thank you again;-) – Tia Nov 27 '15 at 15:31
  • I have a humble request. Could you please upvote my question based on the edits I have made(if you deem it right)? – Tia Nov 27 '15 at 15:32