Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Array and loops.

Hello guys,
So I decided to experiment with what I know.
Start out with a small program then build on it till I used everything I know, with out making any mess.
I came up with an idea of a program that will tell me who's birthday it is and how many days are left for the other peoples birthdays (I used 7 people).

```class BirthdaySurprise {
public static void main(String[] args){
// here are all the people
byte TSE = 122;
short LA = 184;
short ZE = 195;
short AB = 199;
int DE = 234;
int HA = 349;
int AL = 361;
//That's the last one. The numbers are the number of the day that
//each person's birthday comes on. So if your birthday comes on Jan 1
//your variable will be equal to 1, and if on Dec31 it will be equal to 365.
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
System.out.println(aryOne[0]);
}

}
}```
Output:
run:
0
0
0
0
0
0
0
0
0
0
0( 121 zero's in all )

What went wrong in that loop?

Oh and the aryOne array is made in the for loop, that means I wont be able to use it out of the loop right?

2. ## Re: Array and loops.

byte[] aryOne = new byte[aryOneAdder]; // Create a new byte array with aryOneAdder elements

Your loop creates a new array every time it loops. That replaces the one that was created on the last loop

store all the numbers in the aryOne array
To do that create the array of the size you want, OUTSIDE the loop and then in the loop assign each value to an element of the array:
aryOne[aryOneAddr] = aryOneAddr; // assign value to array element: value equals the index

( 121 zero's in all
The default value for an int array element is 0

3. ## Re: Array and loops.

Like this?
```byte aryOneAdder;
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}
byte[] aryOne = new byte[123];
System.out.println(aryOne[0]);```

That gave me an out of bounds error message.

4. ## Re: Array and loops.

Any clue to what woke up indexOutOfBounds?

5. ## Re: Array and loops.

There is important info in the error message, please copy and paste here the full text of the message.

It looks like you create the array AFTER you try to use it.
Create the array BEFORE you use it.

6. ## Re: Array and loops.

After turning of the pc for a few hours and trying again oddly the output has changed.
Output:

run:
0
BUILD SUCCESSFUL (total time: 1 second)

Even before changing the place of the array.

7. ## Re: Array and loops.

Magic. Or you've got a ghost changing your program.

8. ## Re: Array and loops.

My guess: last time a virus messed up Netbeans I uninstalled it and re-downloaded it, but looks like some crazy files where left behind somewhere. :/ I'm planing on uninstalling all programs and downloading them again, might be the registry playing games on me but lets not hope its that.

Anyway it still prints out 0, and not 1-121, you know why?

P.S. If you know any program that can fix the registry leave a link plz.

9. ## Re: Array and loops.

Try playing computer with your program to see how how the values of the variables change.
Go thru the code line by line and record what variables are set to what values.

At the end what is the value in aryOne[0] ?
Look at where you create the aryOne array. When its created all its element values are 0

10. ## The Following User Says Thank You to Norm For This Useful Post:

Melawe (July 31st, 2010)

11. ## Re: Array and loops.

Thanks! That sure is a different way of looking at it!

12. ## Re: Array and loops.

Should I find something strange in the output of
```byte aryOneAdder;
byte[] aryOne = new byte[123];
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}

System.out.println(aryOne[0]);```
and
```for(aryOneAdder = 1; aryOneAdder <121;){
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}

System.out.println(aryOne[0]);]```
and
```byte aryOneAdder;
byte[] aryOne = new byte[123];
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}
System.out.println(aryOne[0]);```
all being

run:
0
BUILD SUCCESSFUL (total time: 1 second).

I have under-looked something or Kaspersky needs reinstalling.

13. ## Re: Array and loops.

To see what is in the array after your code executes use this:

System.out.println(Arrays.toString(aryOne)); // Show array's contents

14. ## The Following User Says Thank You to Norm For This Useful Post:

Melawe (July 31st, 2010)

15. ## Re: Array and loops.

```class BirthdaySurprise {
public static void main(String[] args){
// here are all the people
byte TSE = 122;
short LA = 184;
short ZE = 195;
short AB = 199;
int DE = 234;
int HA = 349;
int AL = 361;
//That's the last one. The numbers are the number of the day that
//each person's birthday comes on. So if your birthday comes on Jan 1
//your variable will be equal to 1, and if on Dec31 it will be equal to 365.
byte[] aryOne = new byte[123];
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}
System.out.println(Array.toString(aryOne)); // Show array's contents
}
}```

output:
run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at BirthdaySurprise.main(BirthdaySurprise.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Output with the print line statement in the loop:

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at BirthdaySurprise.main(BirthdaySurprise.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

16. ## Re: Array and loops.

What source line is at line 21?
What import statements do you have in your program?
You need to import the package that the Arrays class is in?

17. ## Re: Array and loops.

What is the import statement should I use?

18. ## Re: Array and loops.

Originally Posted by Melawe
What is the import statement I should use?
I assume you wish to use the Arrays (Java Platform SE 6) class and not Array class...see the package name in the link

19. ## Re: Array and loops.

import java.util.Arrays;

20. ## Re: Array and loops.

```import java.util.Arrays;
class BirthdaySurprise {
public static void main(String[] args){
// here are all the people
byte TSE = 122;
short LA = 184;
short ZE = 195;
short AB = 199;
int DE = 234;
int HA = 349;
int AL = 361;
//That's the last one. The numbers are the number of the day that
//each person's birthday comes on. So if your birthday comes on Jan 1
//your variable will be equal to 1, and if on Dec31 it will be equal to 365.
byte[] aryOne = new byte[123];
//Now what I want to happen here is the for loop increments aryOneAdder
//till it's equal too 121. And store all the numbers in the aryOne array
}
System.out.println(Arrays.toString(aryOne)); // line 23
}
}```

Output
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] all zeros :/

Sorry for my late late reply.

21. ## Re: Array and loops.

What is the contents of: aryOne[1] ?
You are copying it into all of the elements of the array?
If it contains 0 then your program is working as I'd expect it to.

22. ## Re: Array and loops.

All elements of aryOne are equal to 0.

23. ## Re: Array and loops.

Note: this is another program I use to experiment something I am not sure will work, if it works I then apply it to the main program.

```class Paster {
public static void main(String[] args){
}
}

}
}```

I tried this and got this output:
run:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 to 121.

24. ## Re: Array and loops.

Ok. Is your problem solved now?

25. ## Re: Array and loops.

Not reallyx cause the output of the BirthdaySurprise program is still 0.

26. ## Re: Array and loops.

??? The last post showed that the output was the numbers from 1 to 121

27. ## Re: Array and loops.

That was the output for the Paster program, which I was using to experiment with. But the Birthday Surprise program still has 0 as the output.

Page 1 of 2 12 Last