Sort array alphabetically and show only first name of each item

Asked

Viewed 616 times

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?

  • Yes, you’ll only have one space.

1 answer

4


Try this way:

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);

    String[] firstNames = new String[nome.length];

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

        firstNames[i] = nome[i].split("\\s")[0];         
    }

    for(String firstName : firstNames){
        System.out.println(firstName);
    }
}

The split breaks the String in a new string array, with the name and surname (which are the values separated by the space). The first input of this array is the name.

See working on IDEONE.

Nothing prevents you from displaying the names directly in the first loop, but as the exercise requires a new name-only array, I did the first to separate and store the names, and the second to display them.

  • Just to comment on the regex. \s means any blank space (space or \t\n\x0B\f\r). If you expect more than one space to appear between the name and surname it is possible to use \s+ which means one or more spaces.

  • @Anthonyaccioly Truth. I focused only on the exercise statement, which cites a space as separator. But on another occasion, I would check spaces even at the beginning of the name, since it is a user input.

  • @I smoke what error? Click on the IDEONE link and run, it is working normally. Anything, you can copy from there, I just added the above in your code.

  • @Smoke worked right there?

  • Hello friend, I could see where I was wrong, only at the time of testing the program and printing the names keeps appearing null in the list of first names, how to fix it?

  • @I smoke your code or the link?

  • @diegofm my code.

  • I tested with your code and also keeps appearing null

  • @Fumero have you executed the code on the IDEONE link? There is no null displayed. See if you have not changed anything there in the code that affected it.

  • Only thing I put was the name of the package since I’m using the eclipse

  • @Fumero post your code as it is now (displaying that null) or in ideone or Pastebin and paste the link here in the comments.

  • Here is the code of Pastebin: http://pastebin.com/4dmYiM7h

  • Here the final result: Enter the name of the 1st student: Marcelo Augusto Enter the name of the 2nd student: Renan Alves Enter the name of the 3rd student: Monica Carvalho Marcelo null null Marcelo Monica null Marcelo Monica Renan

  • @Fumero you had placed the third loop inside the second, when they are independent links(the second removes the surname and populates the array of names, the third only displays this array), the correct is like this: http://pastebin.com/56yDP0p7

  • Dude, I didn’t even notice the wrong position of the key that closes the second loop. Thanks really pal, helped a lot in this exercise, I hit my head with it. Hug.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.