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.


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


Post a Reply


- page 1
Oldest  Newest  Rating
+1 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

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.


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);
+1 Developer John · November 23, 2014
Are you seriously insulting Bucky, yet on HIS website? Yes, I've tried many books in the past, and could never understand the authors. You should know that not everyone uses the same resource, because people learn in different ways. Not only insulting Bucky, but why would you even attempt to be mad at me? Am I specifically asking for code? No, I'm asking questions on things. Do you have a problem with me asking questions frequently? Yes, you do, don't lie about it. The more questions a programmer asks, the more he/she understands the concept. Have you even attempted to look at my logical definitions above? They're basically the same stated from Oracle and Bucky's videos. Oracle is my favorite resource, because I can actually understand something from them rather than books, which is why I don't read Java books anymore. Now the ":" used in code I tend to find is different for me. All I asked about it here was if I had the definition right and also what it's used in. If you're going to just stay here and keep on complaining about my questions, you might as well leave, because I'm NOT letting you make me quit learning. I've already done that two times in the past. It's not happening again. 
0 Developer John · November 24, 2014
If you're reading this comment right now, remember that the comment above this was told to someone other than Homer, or Steve. It was specifically to a person that kept on disrespecting my learning practices. Anyways, if you see any confusion in this post, those might have been replies to him/her. The post above shows a brief description of me telling this person's wrongdoings.;)
0 Developer John · November 20, 2014
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 22, 2014
Okay, so right now I'm trying to understand Enumeration, the : for enhanced for loops, ternary operators, and also what instanceof means. These are my logical guesses:

1. Enumeration- a data type used to declare constants? What would be a constant in Java?
2. The ":" usage- is a reference to an object?
3. Ternary Operators- I have no clue, but they're obviously operators lol.
4. Instanceof- compares an object to a type?

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 22, 2014
The ternary operator is pretty much an if statement.

if(b > 10)
  a = b;
  a = c;

That can be written using the ternary operator like so.

a = b > 10 ? b : c;

The condition to be checked.
If the condition is true.
if the condition is false.

the expression before the question mark is the if condition if that's true than whats after the question mark is the value of a. The ":" is used if the expression is false. and what follows it will be the value of a.

Java / Android Development


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

Bucky Roberts Administrator