Aaron Barnard
Aaron Barnard

Reputation: 127

Why doesn't my array work?

I'm very new to java (only been using it for 2 days now), and am trying to make a class that lets you input three numbers, and then outputs the average of all three. When the code is like this the output always equals 0 and I don't know why? I am able to get it to work if I change add "static" to all the public integers, but why do I have to do that? Is there another way I can do it without making them static?

import java.util.Scanner;

public class lettuce 
{
public int num1;
public int num2;
public int num3;

public static void main(String args[])
{

    lettuce lettuceObject = new lettuce();

    int total = 0;
    int average;


    int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};

    lettuceObject.getNum1();
    System.out.println(lettuceObject.num1);
    System.out.println(array[0]);
    lettuceObject.getNum2();
    System.out.println(lettuceObject.num2);
    System.out.println(array[1]);
    lettuceObject.getNum3();
    System.out.println(lettuceObject.num3);
    System.out.println(array[2]);


    for(int counter = 0; counter < array.length;counter++)
    {
        total = total + array[counter];
    }
    average = total/array.length;

    System.out.println("The average of the three numbers is: " + average);



}

public int getNum1()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your first number: ");
    return num1 = keyboard.nextInt();
}
public int getNum2()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your second number: ");
    return num2 = keyboard.nextInt();
}
public int getNum3()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your third number: ");
    return num3 = keyboard.nextInt();   
}

}

Upvotes: 2

Views: 105

Answers (2)

Netto
Netto

Reputation: 284

As you are new I will give you some more tips they making this work.

1: The static modifier specifies that you don't need to instanciate a Class to use that attribute (variable or method ). For example, if you have a class with one static variable:

public class Clazz {
   static int variable=1;
}

You may call it without creating an instance of Clazz. System.out.println(Clazz.variable); would compile with no problems. Otherwise, a non-staticattribute will need an Instance of Clazz to be accessed:

Clazz instanceOfClazz = new Clazz();
System.out.println(instanceOfClazz.variable);

2: The type intis native. So, when you create your array, you are passing no values, and after reading the output, your array is not updated.

3: a double variable would be more precise to store the result of an average.

4: last but not least, your getNum method could be merged into just 1 method receiving the message as parameter, so you hace a best and clear reuse of the code. That can be staticbecause it doesn't need to interact with any object of the class Lettuce (with receive as parameter all it needs to execute and return the integer sent by the user, you can assing the return outside the method)

Ps.: by notation, the class name should start with capital letter.

Your final class would look better this way:

import java.util.Scanner;

public class Lettuce 
{
public int num1;
public int num2;
public int num3;

public static void main(String args[])
{

    Lettuce lettuceObject = new Lettuce();

    int total = 0;
    double average;



    lettuceObject.num1 = lettuceObject.getNum("Please type your first number: ");
    System.out.println(lettuceObject.num1);
    System.out.println(array[0]);
    lettuceObject.num2 = lettuceObject.getNum("Please type your second number: ");
    System.out.println(lettuceObject.num2);
    System.out.println(array[1]);
    lettuceObject.num2 = lettuceObject.getNum("Please type your third number: ");
    System.out.println(lettuceObject.num3);
    System.out.println(array[2]);


    int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};
    for(int counter = 0; counter < array.length;counter++)
    {
        total = total + array[counter];
    }
    average = total/array.length;

    System.out.println("The average of the three numbers is: " + average);



}
public int getNum(String message)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println(message);
    return keyboard.nextInt();
}
}

Hope this helped.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

The output is 0 because you have never initialized your num(s), you're assigning to them on get(s) which you never call - and you're trying to set them in get(s) which isn't the customary approach.

public int num1 = 3;
public int num2 = 3;
public int num3 = 3;

And you should get 3. A getter should look like

public int getNum1()
{
    return num1;
}

A setter should look like

public void setNum1(int num1) {
    this.num1 = num1;
}

And then you would customarily name your class Lettuce and call it from main like

Lettuce lettuce = new Lettuce();
lettuce.setNum1(10);
System.out.println(lettuce.getNum1());

You would customarily also make your fields private and access them through your mutator and accessor methods (getters and setters)

private int num1;
private int num2;
private int num3;

You could choose to create a constructor

public Lettuce(int num1, int num2, int num3) {
    this.num1 = num1;
    this.num2 = num2;
    this.num3 = num3;
}

You could also calculate the average from "lettuce" with something like

public double average() {
    return (num1 + num2 + num3) / 3.0;
}

Edit

Please don't edit your question like that. Also, consider the order of your operations. Your get methods are what set the values. So call them before you create your array!

lettuceObject.getNum1();
lettuceObject.getNum2();
lettuceObject.getNum3();
// Each of those values is 0 until you call the previous three lines.
int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);

Upvotes: 1

Related Questions