3
I am totally layman in Java, wanted help to finalize an exercise.
The statement is this:
"write a program that receives an array with the name complete of 10 people and present an array with only the first name of each person and in this array of only names they should be listed in alphabetical order."
Example: Joao da Silva, Felipe Santos, Adriano Kramer.... (first array) . Result: Adriano, Felipe , Joao ...(second array).
Assume only the first name before space as the first name!
I already managed to write the ten names part (I put 3 in the code to speed up the tests, then I change to 10).
I’m having a hard time finishing the part that shows only the first name. The teacher said he could use the command split()
, I still couldn’t do this part of the exercise.
Just follow my code:
package exercicio2;
import java.util.Scanner;
import java.util.Arrays;
public class ListaNomes {
public static void main(String[] args ) {
Scanner input = new Scanner(System.in);
String nome[] = new String[3];
for(int i = 0; i<nome.length; ++i) {
System.out.print("Digite o nome do " +(i+1) + "º aluno: ");
nome[i] = input.nextLine();
}
System.out.println(" ");
Arrays.sort(nome);
for (int i=0; i< nome.length; i++){
System.out.print(nome[i]+"\n");
}
input.close();
}
}
Between name and surname will always have only one space?
– user28595
Yes, you’ll only have one space.
– Fumero