0

The question is that write a class named Seyyed includes a method named seyyed. I should save the name of some people in a String array in main method and calculate how many names begin with "Seyyed". I wrote the following code. But the output is unexpected. The problem is at line 10 where the sentence "Enter a name : " is printed two times at the first time.

import java.util.Scanner;

public class Seyyed {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of names :");
        int n = in.nextInt();
        String[] names = new String[n];
        for (int i = 0; i < names.length; i++) {
            System.out.println("Enter a name : ");
            names[i] = in.nextLine();
        }
        int s = seyyed(names);
        System.out.println("There are " + s + " Seyyed");
        in.close();
    }

    static int seyyed(String[] x) {
        int i = 0;
        for (String s : x)
            if (s.startsWith("Seyyed"))
                i++;
        return i;
    }

}

for example When I enter 3 to add 3 names the program 2 times repeats the sentence "Enter a name : " and the output is something like this:

Enter the number of names :3
Enter a name : 
Enter a name : 
Seyyed Saber
Enter a name : 
Ahmad Ali
There are 1 Seyyed

I can enter 2 names while I expect to enter 3 names.

4 Answers4

4

The problem occurs as you hit the enter key, which is a newline \n character. nextInt() consumes only the integer, but it skips the newline \n. To get around this problem, you may need to add an additional input.nextLine() after you read the int, which can consume the \n.

Right after in.nextInt(); just add in.nextLine(); to consume the extra \n from your input. This should work.

Original answer: https://stackoverflow.com/a/14452649/7621786

Mo Younis
  • 106
  • 4
2

When you enter the number, you also press the Enter key, which does an "\n" input value, which is captured by your first nextLine() method. To prevent that, you should insert an nextLine() in your code to consume the "\n" character after you read the int value.

Scanner in = new Scanner(System.in);
        System.out.print("Enter the number of names :");
        int n = in.nextInt();
        in.nextLine();
        String[] names = new String[n];

Good answer for the same issue: https://stackoverflow.com/a/7056782/4983264

Fenyxu
  • 21
  • 1
  • 3
0

nextInt() will consume all the characters of the integer but will not touch the end of line character. So when you say nextLine() for the first time in the loop it will read the eol left from the previous scanInt(), so basically reading an empty string. To fix that use a nextLine() before the loop to clear the scanner or use a different scanner for Strings and int.

anthony yaghi
  • 395
  • 1
  • 7
0

Try this one:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the number of names :");
    int n = in.nextInt();
    in.nextLine();
    String[] names = new String[n];
    for (int i = 0; i < names.length; i++) {
        System.out.println("Enter a name : ");
        names[i] = in.nextLine();
    }
    int s = seyyed(names);
    System.out.println("There are " + s + " Seyyed");
    in.close();
}

static int seyyed(String[] x) {
    int i = 0;
    for (String s : x)
        if (s.startsWith("Seyyed"))
            i++;
    return i;
}
Sana
  • 362
  • 3
  • 13