# Array and loops.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• July 29th, 2010, 19:43
Melawe
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).

Code Java:

```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. for( byte 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 aryOneAdder++; byte[] aryOne = new byte[aryOneAdder]; 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?
• July 29th, 2010, 19:53
Norm
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

Quote:

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

Quote:

( 121 zero's in all
The default value for an int array element is 0
• July 29th, 2010, 21:48
Melawe
Re: Array and loops.
Like this?
Code Java:

```byte aryOneAdder; 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 aryOneAdder++; } byte[] aryOne = new byte[123]; aryOne[aryOneAdder] = aryOneAdder; System.out.println(aryOne[0]);```

That gave me an out of bounds error message.
• July 29th, 2010, 22:50
Melawe
Re: Array and loops.
Any clue to what woke up indexOutOfBounds?
• July 30th, 2010, 05:45
Norm
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.
• July 30th, 2010, 13:15
Melawe
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.
• July 30th, 2010, 13:33
Norm
Re: Array and loops.
Magic. Or you've got a ghost changing your program.
• July 30th, 2010, 16:03
Melawe
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. :P

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. :)
• July 30th, 2010, 16:18
Norm
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
• July 30th, 2010, 21:14
Melawe
Re: Array and loops.
Thanks! That sure is a different way of looking at it!
• July 30th, 2010, 21:29
Melawe
Re: Array and loops.
Should I find something strange in the output of
Code Java:

```byte aryOneAdder; byte[] aryOne = new byte[123]; 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 aryOneAdder++; aryOne[0] = aryOne[aryOneAdder]; }   System.out.println(aryOne[0]);```
and
Code Java:

```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 aryOneAdder++; aryOne[aryOneAdder] = aryOne[0]; }   System.out.println(aryOne[0]);]```
and
Code Java:

```byte aryOneAdder; byte[] aryOne = new byte[123]; 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 aryOneAdder++; } aryOne[aryOneAdder] = aryOneAdder; System.out.println(aryOne[0]);```
all being

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

I have under-looked something or Kaspersky needs reinstalling.:-t
• July 31st, 2010, 06:30
Norm
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
• July 31st, 2010, 21:22
Melawe
Re: Array and loops.
Code java:

```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 aryOneAdder; byte[] aryOne = new byte[123]; 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 aryOneAdder++; aryOne[0] = aryOne[aryOneAdder]; } 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)
• August 1st, 2010, 06:36
Norm
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?
• August 1st, 2010, 10:30
Melawe
Re: Array and loops.
What is the import statement should I use?
• August 1st, 2010, 10:48
copeg
Re: Array and loops.
Quote:

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
• August 1st, 2010, 13:11
Norm
Re: Array and loops.
import java.util.Arrays;
• August 4th, 2010, 11:20
Melawe
Re: Array and loops.
Code Java:

```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 aryOneAdder; byte[] aryOne = new byte[123]; 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 aryOneAdder++; aryOne[aryOneAdder] = aryOne[1]; } 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.
• August 4th, 2010, 11:34
Norm
Re: Array and loops.
Quote:

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.
• August 4th, 2010, 11:38
Melawe
Re: Array and loops.
All elements of aryOne are equal to 0.
• August 4th, 2010, 11:41
Melawe
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.

Code Java:

```class Paster { public static void main(String[] args){ byte aryOneAdder; for(aryOneAdder = 0; aryOneAdder <121;){ if (aryOneAdder <121){ aryOneAdder++; } System.out.println(aryOneAdder); }   } }```

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.
• August 4th, 2010, 11:52
Norm
Re: Array and loops.
Ok. Is your problem solved now?
• August 4th, 2010, 11:56
Melawe
Re: Array and loops.
Not reallyx cause the output of the BirthdaySurprise program is still 0.
• August 4th, 2010, 12:22
Norm
Re: Array and loops.
??? The last post showed that the output was the numbers from 1 to 121
• August 4th, 2010, 12:25
Melawe
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last