-3

Hello i have been trying to make a program THAT ASK 1)How many students are there?? 2)Asks a name and Test marks

can somebody help me with this . Im new to programming here is my code

Scanner input = new Scanner(System.in);

System.out.println("How many Students in class?");
int n = input.nextInt(); 
System.out.println("Enter the " + n + " names: ");

String [] names = new String[n];

for (int i = 0; i < names.length; i++)
{
    names[i] = input.nextLine();    
}  
Cory Kramer
  • 98,167
  • 13
  • 130
  • 181
k.joe
  • 21
  • 3
  • 2
    k.joe, welcome to StackOverflow. I guess you'll be getting downvotes to this question because it does not adhere to the spirit and rules of the site. It is good that you have put up the question, but please make sure you present what problems/errors you are facing in your code, and ask solutions for that. Please do not expect people to write logic for you. – Vinay Rao Feb 10 '17 at 13:04
  • Welcom, please, see [ask], you didn't explain your problem. This is needed to get a quick answer (here it is obvious when you now the problem) – AxelH Feb 10 '17 at 13:09
  • 1
    Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – YCF_L Feb 10 '17 at 13:17

1 Answers1

1

It is a "problem" with nextInt, the method only read the number and leave the <enter> so the next time you call nextLine, the <enter> being present, it will read this, so the value is empty. You need to clear the input first, simply by reading the line.

System.out.println("How many Students in class?");
int n = input.nextInt(); 
input.nextLine(); //will consume the \n
AxelH
  • 13,322
  • 2
  • 21
  • 50