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?

Jonathan Cook
  • 55
  • 2
  • 7
  • is it getting the right input? can you check if the name of student 2 and it's grades are right? – Frowner Jun 20 '15 at 15:25
  • possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Jun 20 '15 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. – Jimmy Jun 20 '15 at 15:43

4 Answers4

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.

Tom
  • 14,120
  • 16
  • 41
  • 47
Saleh Parsa
  • 1,362
  • 12
  • 22
  • 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. – Tom Jun 20 '15 at 15:36
  • @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). – Tom Jun 20 '15 at 15:47
  • @Tom I've tested this code and it worked in my end. I've no idea why I got a down vote when it works. Would be awesome if you give me some explanation :-) – Saleh Parsa Jun 20 '15 at 16:32
  • @SudhansuChoudhary I haven't changed initial code :-) – Saleh Parsa Jun 20 '15 at 16:32
  • Cool!! @Tom the code worked at my end too.. @SalehParsa I understand, all I was concerned was if syso("") would make any difference as I had just then posted my answer, which I found out it wouldn't.. hey, anyways why do u wanna do `Name[k] = input1.nextLine();` ?? just a `input1.nextLine()` would work right?? It might create some confusion as in you are assigning a value to `Name[k]` twice in the while loop.. It works all right, I get it but just a thought!! :-) – Sudhansu Choudhary Jun 20 '15 at 16:58
  • 1
    @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. – Tom Jun 20 '15 at 17:14
  • 1
    @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 Jun 20 '15 at 17:26
  • 1
    @Tom You are right! I edited my answer :-) Thanks for the headsup! – Saleh Parsa Jun 20 '15 at 17:32
  • I guess you already know that the last `nextLine` should read the remaining "line delimiter" of the user input, since `nextDouble` doesn't consume it. Since you've assigned that to `Name[k]` you replaced the entered name with an empty String. But you've fixed that now :). – Tom Jun 20 '15 at 17:39
  • @Tom your approaches to my answer has been similar to this quote: "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime" :-) I learned from it Thanks mate :-) – Saleh Parsa Jun 20 '15 at 17:41
  • @Tom yes I checked the Name array after the while loop, yeah it does not cause trouble, it causes "huge" trouble, good that the answer's been edited, thanks for your headsup. So @SalehParsa your code and my code is same now except that `System.out.println("")` :-D – Sudhansu Choudhary Jun 20 '15 at 17:50
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);
Frowner
  • 644
  • 6
  • 19
  • 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 Jun 20 '15 at 15:40
  • @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 :) – Frowner Jun 20 '15 at 15:44
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.

Community
  • 1
  • 1
Sudhansu Choudhary
  • 3,095
  • 2
  • 15
  • 28
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++;
}
Ludovic Feltz
  • 10,007
  • 3
  • 43
  • 51
  • 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 – Saleh Parsa Jun 20 '15 at 15:23
  • Makes more sense buddy :-) – Saleh Parsa Jun 20 '15 at 15:31
  • *"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. – Tom Jun 20 '15 at 15:37