简体   繁体   中英

Loop->Array Output

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?

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.

the nextInt of the Scanner does 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);

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 .

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++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM