1

I'm writing a loop to fill an array. I think I have the coding down, but when I run the compiled code through Java, it doesn't come out right in the command prompt.

Here's the code:

import java.util.Scanner;
import java.io.*;
public class Pr42
{
    public static void main(String[]args) throws IOException
{
    int k,m,g;
    String n;
    //double g;
    Scanner input1=new Scanner(System.in);
    String[]Name=new String [5];
    double[]Grade=new double[Name.length];
    k=0;
    while (k<Name.length)
        {
        m=k+1;
        System.out.print("Enter the name of student "+m+": ");
        Name[k]=input1.nextLine();
        System.out.print("");
        System.out.print("Please enter the grade of student "+m+": ");
        Grade[k]=input1.nextInt();
        k++;
        }
    }
}

Here's the output in the command prompt:

Enter the name of student 1:

Please enter the grade of student 1:

Please enter the name of student 2: Please enter the grade of student 2:

The problem is that line regarding the second student.

What did I do wrong in the code to get an output like that?

3
  • is it getting the right input? can you check if the name of student 2 and it's grades are right? Commented Jun 20, 2015 at 15:25
  • possible duplicate of Scanner issue when using nextLine after nextXXX Commented Jun 20, 2015 at 15:25
  • bringing the scanner declaration inside loop should fix it too. But my preference would be to read nextLine() instead of nextInt() for grade and parse it/handle exception accordingly. Commented Jun 20, 2015 at 15:43

4 Answers 4

1

You need to identify name has input in next line with Name[k] = input1.nextLine();

int k, m, g;
String n;
//double g;
Scanner input1 = new Scanner(System.in);
String[] Name = new String[5];
double[] Grade = new double[Name.length];
k = 0;
while (k < Name.length) {
    m = k + 1;
    System.out.print("Enter the name of student " + m + ": ");
    Name[k] = input1.nextLine();
    System.out.print("");
    System.out.print("Please enter the grade of student " + m + ": ");
    Grade[k] = input1.nextDouble();
    input1.nextLine();
    k++;
}

EDITED: As per mentioned in Tom's comment under this answer when you have Name[k] = input1.nextLine(); instead of input1.nextLine(); program works correctly, but it messed up with value of array.

Sign up to request clarification or add additional context in comments.

11 Comments

I wonder why OP accepted this answer. Haven't both, Saleh and OP tested this code? As a hint, you should print the content of Name after the while loop.
@SudhansuChoudhary The statement System.out.print(""); is adopted from OPs original code and it has no point here. I guess Saleh didn't touched that line, because he doesn't intended to optimize that code (which is understandable).
@SalehParsa "I've tested this code and it worked in my end." Let me guess, you just run the code right? You didn't check if the content of both arrays are correct? Well, you should do that.
@SudhansuChoudhary "It might create some confusion as in you are assigning a value to Name[k] twice in the while loop.. It works all right" No it doesn't. The second assignment is wrong and it causes trouble. You could notice that if you check the content of the Name array right after the while loop.
@Tom You are right! I edited my answer :-) Thanks for the headsup!
|
1

the nextInt of the Scannerdoes not read the "new line" character.
there are 2 ways to fix it.
1. call input1.nextLine(); after the input1.nextInt(); ignore what you get here, it is just to make it go to the next line.

Grade[k] = input1.nextInt();
input1.nextLine();

2. call input1.nextLine(); for the grade. the String you get you can cast to int and save in Grade[k].

String str = input1.nextLine();
Grade[k] = Integer.parseInt(str);

2 Comments

There is also another way (let's call it 2.5 :D): OP can use Name[k]=input1.next() instead of Name[k]=input1.nextLine();. This avoids the mentioned problem, but each name can either be a firstname or a surname, since next() stops to read on a whitespace.
@Tom yes. i don't suggest it because when i started using java, i used the next(); and stayed with it without knowing his disadvantage (read until white space). i ended up with an answer and a lot of wasted time to figure it up. anyway, good idea :)
1

This works:

public static void main(String[] args) throws IOException {
    int k, m, g;
    String n;
    // double g;
    Scanner input1 = new Scanner(System.in);
    String[] Name = new String[5];
    double[] Grade = new double[Name.length];
    k = 0;

    while (k < Name.length) {

        m = k + 1;
        System.out.print("Enter the name of student " + m + ": ");
        Name[k] = input1.nextLine();
        System.out.print("Please enter the grade of student " + m + ": ");
        Grade[k] = input1.nextInt();
        input1.nextLine();
        k++;
    }
}

I recommend you to please go through this question, you shall have your doubts clarified. Excerpt from the answer given in that post:

Scanner#nextInt method does not read the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine.

Comments

0

The problem is that: Grade[k] = input1.nextInt(); don't reads the end of line or anything after the number.

Try placing a input1.nextLine(); after Grade[k]=input1.nextInt(); should solve the issue:

while (k<Name.length)
{
        m=k+1;
        System.out.print("Enter the name of student "+m+": ");
        Name[k]=input1.nextLine();
        System.out.print("");
        System.out.print("Please enter the grade of student "+m+": ");
        Grade[k]=input1.nextInt();
        input1.nextLine();
        k++;
}

3 Comments

Even with println it's still showing wrong output. It will add a new line after the last input it doesn't fix initial problem
Makes more sense buddy :-)
"Try placing a input1.nextLine(); after Grade[k]=input1.nextInt(); should solve the issue" And why? Since OP doesn't know what the problem is, a fix like this should have an explanation, why it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.