Tuesday, November 22, 2011

Working with java String.split() method

PROBLEM
Ross is an event organizer. He has received data regarding the participation of employees in two different events. Some employees have participated in only one event and others have participated in both events. Ross now needs to count the number of employees who have taken part in both events. The records received by Ross consist of employee ids, which are unique. Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events.

Suppose the following input is given to the program, where each line represents a different event:

1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112

Now the common employee ids are 1003 and 1005, so the program should give the output as:

2

Solution via Simple java code

import java.util.Scanner;
class problem3{
public static void compare(String id1[],String id2[]){
int k=0;
for(int i=0;i<id1.length; i++){
int x = Integer.parseInt(id1[i]);
for(int j=0; j<id2.length; j++){
int y = Integer.parseInt(id2[j]);
if(x==y){
k++;
}
}
}
System.out.println(k);
}
public static void main(String args[]){
   String firstLine;
String secondLine;
   Scanner in = new Scanner(System.in);
firstLine = in.nextLine();
secondLine = in.nextLine();
String delimiter = "\\,";
String[] array1 = firstLine.split(delimiter);    // array1{1001,1002,1003,1004,1005};
       String[] array2 = secondLine.split(delimiter);  // array2{1106,1008,1005,1003,1016,1017,1112};
compare(array1, array2);
}
}

Note: Input given via Console input(using scanner class).
After run this code via command prompt, you can type values and then enter. now you can see the output.

2 comments:

  1. Grammar, vocabulary, tenses, indirect speech, passive sentences must always be keep in mind while writing a blog. Everyone must read this blog. This is going to help everyone.
    aromatherapy

    ReplyDelete
  2. Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.
    Guest posting sites
    Technology updates

    ReplyDelete