0

This is a basic name sorting program. Everything works except for the fact that the user cannot input the first name. This is the code:

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    for (int x = 0; x < names.length; x++){
        int pos = x+1;
        System.out.println("Enter name " + pos);
        //String temp = sc.nextLine();
        names[x] = sc.nextLine();
    }
    String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++){
        System.out.print(sortedArray[i] + " ");
    }
}   

Update: I changed the code so if it is the first time, it calls sc.nextLine() and then sets the input equal to names[0] One problem with .next() is that if a person's first name is 2 words to treats it as two names. This is the updated code that works:

public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    //String[] temp = new String[names.length];
    for (int x = 0; x < names.length; x++) {
        int pos = x + 1;
        if (x == 0) {
            System.out.println("Enter name 1");
            sc.nextLine();
            names[0] = sc.nextLine();
        } else {
            System.out.println("Enter name " + pos);
            //String temp = sc.nextLine();
            names[x] = sc.nextLine();
        }
    }
    String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++) {
        System.out.print(sortedArray[i] + " ");
    }
}

1 Answers1

2

Use sc.next(); instead of sc.nextLine();

  • next() will find and return the next complete token from the input stream.
  • nextLine() will advance the scanner past the current line and will return the input that was skipped

Also, check below description from Scanner#nextLine().

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

 Scanner sc = new Scanner(System.in);
    System.out.println("How many names do you want to sort");
    int num = sc.nextInt();
    String[] names = new String[num];
    for (int x = 0; x < names.length; x++){
        int pos = x+1;
        System.out.println("Enter name " + pos);
        //String temp = sc.nextLine();
        names[x] = sc.next();
    }
    /*String sortedArray[] = sort(names);
    for (int i = 0; i < sortedArray.length; i++){
        System.out.print(sortedArray[i] + " ");
    }*/
hagrawal
  • 12,025
  • 4
  • 33
  • 61
  • Have you got your answer, if not then please write your answer so that other's can be benefited from it. stackoverflow.com/help/accepted-answer – hagrawal Jul 01 '15 at 20:32