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
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.;)
+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 23, 2014
For instance, an enhanced for loop.
0 Homer Simpson · November 22, 2014
It might be used for other things but that's the only way I know how to use it. :/ 
0 Developer John · November 22, 2014
Are you sure the ":" is only for true or false statements? I've seen it used in several different occasions before, but can't remember how exactly they were used.
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.
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 20, 2014
-1 Developer John · November 20, 2014
Ok, lets just say I have a class and a method:
class Test{
public static void Eat(){

Why wouldn't I have to make an object for the class and then get the method?
0 Homer Simpson · November 19, 2014
I like to initialize my variables when I declare them. Just good practice in my opinion. 
average += Array[counter] is the same as
average = average + Array[counter];

So the first time we loop through I'm setting "average" equal to whatever the user enters in.  Say they enter in 100. So now average is equal to 100. Next time around I will be adding what the user enters in to 100. Say they entered in 50. Average would be equal to 150.

average /= Array.length; again thats the same as
average = average / Array.length;

When I declared my array int Array = new int[2];
I told java that my array length is going to be 2.

So average = average / Array.length is the same as
average = 150 / 2

The reason I did it like that is say for example I wanted to get the average of three numbers. I could just change my Array size to 3
int Array = new Array[3] and thats all I would have to do. The code still works.

Java / Android Development


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

Bucky Roberts Administrator