0

Hi there I am super new to coding and I keep getting a '.class' error when I try to run the code below. What am I missing?

import java.util.Scanner;

import java.util.Scanner;


public class PeopleWeights {
    public static void main(String[] args) {
        Scanner scnr = new Scanner (System.in);
        userWeight = new int[5];
        int i = 0;

        userWeight[0] = 0;
        userWeight[1] = 5;
        userWeight[2] = 6;
        userWeight[3] = 7;
        userWeight[4] = 9;

        System.out.println("Enter weight 1: ");
        userWeight = scnr.nextInt[];

        return;
    }
}
1
  • 4
    "userWeight = scnr.nextInt[];" - Those are the wrong kind of brackets. Use (). That will fix one of your problems. Commented Oct 5, 2016 at 18:58

3 Answers 3

1

This is the problem

userWeight = scnr.nextInt[];

Solve this by:

userWeight[0] = scnr.nextInt();        //If you intended to change the first weight

OR

userWeight[1] = scnr.nextInt();        //If you intended to change the value of userWeight at index 1 (ie. the second userWeight)

Should work

PS: As a precaution do not import the Scanner class twice. Doing it once would be enough

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

1 Comment

It isn't a "precaution" to import only once, simply a cleanup.
0

I understood your intension and below are two possible ways to implement your thought:

I see you are giving values manually as userWeight[0]=0; If you wanna give manually I suggest not to go with scanner as below.

public static void main(String[] args) {
     int[] userWeight={0, 5, 6,7,9};
         System.out.println("Weights are" +userWeight);//as you are giving values.
 }

If your intension is to get values at run time or from user, please follow below approach

 public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("This is runtime and you need to enter input");

        int[] userWeight = new int[5];

            for (int i= 0; i < userWeight.length; i++) {
                userWeight[i] = sc.nextInt();
                System.out.println(userWeight[i]);
            }
        }

PS:

I seen you are using util package import for two times, instead you may import all at once as import java.util.*;

Also you are trying to return. Please note for void methods its not need for return values. VOID excepts nothing in return.

Comments

0

First of all do not import packages more than once, now lets go to the actual "bugs".

Here:

import java.util.Scanner;

public class PeopleWeights {
    public static void main(String[] args) {
         Scanner scnr = new Scanner (System.in);
         int userWeight[] = new int[5];//You need to declare the type
         //of a variable, in this case its int name[]
         //because its an array of ints
         int i = 0;

         userWeight[0] = 0;
         userWeight[1] = 5;
         userWeight[2] = 6;
         userWeight[3] = 7;
         userWeight[4] = 9;

         System.out.println("Enter weight 1: ");
         userWeight[0] = scnr.nextInt();//I belive that you wanted to change
         // the first element of the array here.
         //Also nextInt() is a method you can't use nextInt[]
         //since it doesn't exists
         //return; You dont need it, because the method is void, thus it doesnt have to return anything.

    }

}

Also instead of this:

userWeight[0] = 0;
userWeight[1] = 5;
userWeight[2] = 6;
userWeight[3] = 7;
userWeight[4] = 9;

you can do this during the declaration of an array:

int userWeight[] = {0,5,6,7,9};//instantiate it with 5 integers

Comments

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.