简体   繁体   中英

Do I need to import something here?

I'm a beginner at Java and I'm creating a program that stores the names and weight of students in two different arrays after taking input from the user 30 times. Here's my code:

import java.util.Scanner;
public class bodymass {

    public static void  main(String[] args) {

        Scanner scan=new Scanner(System.in);
        String[] studentName= new String[30];
        System.out.println("Please enter the Names of 30 students");
        // This "i" is your counter.
        for (int i=0; i< studentName.length; i++) {
            studentName[i]= scan.nextLine();}
        System.out.println("Please enter the Weight of 30 students");
        Scanner scan1= new Scanner(System.in);
        int[] studentWeight= new int[30];
        for(int i=0; i<studentWeight.length; i++) {
            studentWeight[i]= scan.nextInt();}
        }

    }

However when I debug it, I get the message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: at bodymass.main(bodymass.java:5)

What does it mean? I would be grateful if somebody guides me through this.

I have tried it for 2 students and ensure that your java class having no errors.

package com.test.practice;

import java.util.Scanner;

public class Bodymass {

public static void main(String[] args) {
     Scanner scan=new Scanner(System.in);
        String[] studentName= new String[2];
        System.out.println("Please enter the Names of 2 students");
        // This "i" is your counter.
        for (int i=0; i< studentName.length; i++) {
           System.out.print("Enter the student name at postion "+i+". "); 
           studentName[i]= scan.nextLine();
        }

        System.out.println("Please enter the Weight of 2 students");
        Scanner scan1= new Scanner(System.in);
        int[] studentWeight= new int[2];

        for(int i=0; i<studentWeight.length; i++) {
            System.out.print("weight of student at position "+i+" "); 
            studentWeight[i]= scan.nextInt();
            }
    }
}

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