I need the println to display only the first result (input[0]) but it is displaying it once for each split. for example, If I split the string into 5 parts, it will print it once more?
input: "Chelsea : Arsenal : 2 : 1"
output: Chelsea
Chelsea
Chelsea
Chelsea
please enter match result:
Scanner sc = new Scanner(System.in);
for (int b=0; b < 5; b++){
System.out.println("please enter match result:");
String s = sc.nextLine();
String input[] = s.split(":"); // parse strings in between the dash character
for(String temp : input ) {
String hometeam = input[0];
String awayteam = input[1];
String homescore = input[2];
String awayscore = input[3];
System.out.println(input[0]);
/*for(int a=0; a<input.length; a++)//length is the property of array
System.out.println(input[a]);
*/
}
}
Move your System.out.println(fruits[0]);
from inside your for loop to outside your for loop like this -
Scanner sc = new Scanner(System.in);
for (int b=0; b < 5; b++) {
System.out.println("please enter match result:");
String s = sc.nextLine();
String input[] = s.split(":"); // parse strings in between the dash character
for(String temp : input ) {
String hometeam = input[0];
String awayteam = input[1];
String homescore = input[2];
String awayscore = input[3];
}
System.out.println(input[0]);
}