6 Questions

+4 Developer John · November 18, 2014
1. What does static mean?
2. What does null mean?
3. What does final mean?
4. Isn't a constructor basically when you want to assign a value to something right before it is used?
5. What is casting and why is it useful?
6. What is wrong with this?

Here, I'm trying to make a program that calculates the average of two variables input by a scanner with text on the screen as well.

Code: 

import java.util.Scanner;

class Test{

public static void main (String [] args){

Scanner scan = new Scanner(System.in);

System.out.println("Enter two numbers so I can calculate the average.");

int Array[] = new int[2];
int average, sum = 0;

for(int counter = 0; counter < Array.length; counter++){

int Input = scan.nextInt();
Array[1] = Input;
int Input2 = scan.nextInt();
Array[2] = Input2;
sum = sum + Array[counter];
}
average = sum / Array.length;
System.out.println("The average of the ten numbers is " + average);

}
}

I have no errors, yet the program throws we an Exception. I would like to know why exactly I'm getting one. The Exception is the ArrayIndexOutOfBoundsException

-Thanks!8-)

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Steve Hammond · November 18, 2014
Your Array can only be indexed using [0] and [1], as it has a bound of 2.
0 Developer John · November 18, 2014
Ok, so I've changed all the numbers so they're decreased by one. It's still not working.
0 Developer John · November 18, 2014
1. I'm not going to read through a hole entire program and not even understand what each basic part of it means.
2. I posted this on the forums because the people here are learning as well, and maybe someone could help.
3. I'm not lazy to use Google, but being told to look it up where you could totally give me help is a huge waste of my time. I'm basically saying this: Would you rather help someone if you know the answer? Or, would you let them stay there without any help, losing a future developer. I would use Google, but I find lots of the information is misleading, and false.
0 Developer John · November 18, 2014
I want to know because basically every tutorial I watch, the person throws these at me and I don't even know what they mean. I have tried Oracle, it's probably one of the best sources I've seen. Sometimes it doesn't fully answer my question.
0 Homer Simpson · November 18, 2014
@Developer John:
1. static means that the variable or method marked as such is available at the class level. In other words, you don't need to create an instance of the class to access it
http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java

2. The way I see it. Null is a value of 0 or nothing.  It is used when a variable exists that could point to an object, but in fact points to no object at that time.

3. Final variables are variable that can't be modified. It will forever be equal to whatever value was given to it. Of course you can go into the code and change it but methods and such can't modify it.

4.A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method: A constructor doesn’t have a return type. The name of the constructor must be the same as the name of the class. Unlike methods, constructors are not considered members of a class. A constructor is called automatically when a new instance of an object is created.

http://www.dummies.com/how-to/content/how-to-use-a-constructor-in-java.html

5. Casting is just taking an object of one type and changing it to another type. This is useful in many cases... lol though I can't really explain why. 

6. As someone mentioned earlier Arrays start at element 0. So just change your values and you stop getting the exception. As you stated earlier that doesn't solve your issue. 
You have a for loop that will loop through 2 times. cool. You want to have the user input 2 numbers and calculate the average of said values. Thats where the issue comes in. 
When we go through your loop the first time we're setting values to Array[0] and Array[1]. But then we go and loop through again and set values to, you guessed it Array[0] and Array[1]... The it continues to calculate the average and display the results... ^_^ I hope you realize where the issue lies. 
Here's my solution :

import java.util.Scanner;

public class Practise { 
     
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
   
    // Prompt the user
System.out.println("Enter two numbers so I can calculate the average.");

// Create an array that will hole two elements. Arrays start at element 0.
int Array[] = new int[2];

int average = 0;

// Loop through two times and set values to our elements
for(int counter = 0; counter < Array.length; counter++){
Array[counter] = scan.nextInt();

// Adds the two numbers that the user enters to the average.
average += Array[counter];
}
// Divide the total of the two numbers the user entered and then divide it by two to get the average.
average /= Array.length;
System.out.println("The average of the ten numbers is " + average);
    }   
}
0 Homer Simpson · November 19, 2014
/images/forum/upload/2014-11-19/550fb71820605444dd2f4f5e776b5021.jpg
0 Developer John · November 19, 2014
Lol thanks Homer!
0 Developer John · November 19, 2014
Wait, actually why did you set the Array element to:
int Array[] = new int[2];


Wouldn't it be 1 since Java counts 0 first?
0 Homer Simpson · November 19, 2014
When you declare an array you're letting Java know how much memory to set aside for the elements in the array. The array itself starts at 0. 
0 Developer John · November 19, 2014
I still don't understand. Doesn't the primitive data type already tell Java how much memory the array needs?

Java / Android Development

107,299 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator