• August 2nd, 2011, 01:02 PM
syang
I'm writing a program that generates 100 random integers from 0 to 25 and stores them in an array. Then, the program should call class methods to sort the odd numbers into an array and the even numbers into another array. This is what I have so far...

Code Java:

```public class U8 { public static void main( String [] args ) {   final int numOfIntegers = 100;   int[] even = new int [numOfIntegers]; int[] odd = new int [numOfIntegers];   int randomNum; int i = 0; int j = 0;   for( int count = 0 ; count < numOfIntegers ; count++ ) { displayEvens(new int[]{Math.random() * 25 + 2}); displayOdds(new int[]{Math.random() * 25 + 2}); }   public static void displayOdds( int randomNum ) { for( count = 0 ; count < i ; count++ ) { if(randomNum / 2 == 1) { i++; displayOdds( randomNum ); } System.out.print( even[count] ); System.out.println( "Odd Numbers: " ); System.out.print( odd[count] ); } }   System.out.println();   public static void displayEvens( int randomNum ) { for( count = 0 ; count < j ; count++ ) { if(randomNum / 2 == 0) { j++; displayEvens( randomNum ); } System.out.print( even[count] ); } }     } }```

When I try to complie it, it keeps telling me ';' expected at public static void displayOdds..... etc and display Evens....
also, '.class' expected at (int randomnum)
and, 'illegal start of expression' at public static void displayOdds/Evens...

What am I typing wrong???
• August 2nd, 2011, 01:11 PM
KevinWorkman
Math.random() is a method, not a variable, so it requires parenthesis after it. PS- You forgot the highlight tags.
• August 2nd, 2011, 01:38 PM
syang
I added parentheses after it, but it still displays the error messages...
• August 2nd, 2011, 02:26 PM
Norm
Quote:

displays the error messages.
Please copy and paste here the FULL text of the error messages. They have info that you have left out.

Check that your {} s are properly matched.
• August 2nd, 2011, 03:12 PM
syang
I can't copy paste it because it is in command prompt and I can't select anything... what should I do?
I just installed netbeans IDE, it often tells me 'reaches end of file while parsing', what does that mean, and does netbeans help?
• August 2nd, 2011, 03:14 PM
Norm
Quote:

I can't copy paste it because it is in command prompt
On windows:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
• August 3rd, 2011, 04:18 PM
syang
Microsoft Windows [Version 6.1.7600]

C:\Users\selena>cd c:\program files\java\jdk1.6.0_21\bin

c:\Program Files\Java\jdk1.6.0_21\bin>javac c:\myjavaprograms\u8.java
c:\myjavaprograms\u8.java:21: illegal start of expression
public static void displayOdds( int randomNum )
^
c:\myjavaprograms\u8.java:21: illegal start of expression
public static void displayOdds( int randomNum )
^
c:\myjavaprograms\u8.java:21: ';' expected
public static void displayOdds( int randomNum )
^
c:\myjavaprograms\u8.java:21: '.class' expected
public static void displayOdds( int randomNum )
^
c:\myjavaprograms\u8.java:21: ';' expected
public static void displayOdds( int randomNum )
^
c:\myjavaprograms\u8.java:38: illegal start of expression
public static void displayEvens( int randomNum )
^
c:\myjavaprograms\u8.java:38: illegal start of expression
public static void displayEvens( int randomNum )
^
c:\myjavaprograms\u8.java:38: ';' expected
public static void displayEvens( int randomNum )
^
c:\myjavaprograms\u8.java:38: '.class' expected
public static void displayEvens( int randomNum )
^
c:\myjavaprograms\u8.java:38: ';' expected
public static void displayEvens( int randomNum )
^
10 errors

c:\Program Files\Java\jdk1.6.0_21\bin>
• August 3rd, 2011, 04:20 PM
Norm
Did you do what I suggested in post#4?
If not, do it now. Check that your {} s are properly matched.

You must have statements outside of a method.
• August 3rd, 2011, 04:21 PM
syang
Actually, I came up with a new edited code, shown below... but when I run it, it gives me unlimited numbers, I'm not sure how to exactly describe it.

Code Java:

```public class Un8 { public static void main( String [] args ) {   int [] even = new int [ 100 ]; int [] odd = new int [ 100 ];   int randomNum;   for( int count = 0 ; count < 100 ; count++ ) { randomNum = (int) (25 * Math.random()); displayEvens( randomNum ); displayOdds( randomNum ); }   }   public static void displayOdds( int randomNum ) { int i = 100; for( int count = 0 ; count < i ; count++ ) { if(randomNum / 2 == 1) System.out.println( randomNum ); { i++;   }   } }     public static void displayEvens( int randomNum ) { int j = 100; for( int count = 0 ; count < j ; count++ ) { if(randomNum / 2 == 0) System.out.println( randomNum ); { j++; }   } }   }```
• August 3rd, 2011, 04:24 PM
Norm
Quote:

it gives me unlimited numbers
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.
• August 3rd, 2011, 05:27 PM
syang
c:\myjavaprograms\java Un8
1
1
1
1
1
1
It just goes on and on, keeps printing out 1 over and over again.
It gives me a different number everytime. Last time it was 0, the first time I ran it was 3.
• August 3rd, 2011, 05:32 PM
Norm
Fives ones and a comment would have been enough to post. That was sort of STUPID of you.
I asked you in post#10 paste some of the console here with comments

Look at what controls the loop where the println was executing. What condition keeps that loop executing? What condition stops the loop?
Is the variable used in the condition test changed inside of the loop?
If it is NOT changed, the condition will be true forever.

Add printouts of the value of i and the value of count and the value of j
• August 3rd, 2011, 05:48 PM
Norm
Sorry, I can't help anyone that is STUPID enough to post that many 0s after being told it was a STUPID thing to do.

I asked you to print out the values of i, j and count. Why didn't you do that?
• August 3rd, 2011, 05:56 PM
syang
sorry, I thought that my replies weren't displaying, until I saw the next page button. I'm new to this site, ok?
This is what it tells me after I added System.out.println( i and j );

c:\Program Files\Java\jdk1.6.0_21\bin>javac c:\myjavaprograms\Un8.java
c:\myjavaprograms\Un8.java:24: variable i might not have been initialized
for( int count = 0 ; count < i ; count++ )
^
c:\myjavaprograms\Un8.java:41: variable j might not have been initialized
for( int count = 0 ; count < j ; count++ )
^
2 errors
• August 3rd, 2011, 08:03 PM
wookyoo
mm i can see some mistakes here.

so you wanna seperate odd and even number, right?

but what is result of 'randomNum / 2 == 1'?

and try to do step by step. like try to make with only one number, not without looping first time.
when you succeed first time then.. make a loop and put function inside of loop.

i guess you want to evaluate random number with display odds or evens method....

is it to evaluate numbers as evens or odds when looping and print out?

then... where should the loop be?
• August 4th, 2011, 07:37 AM
KevinWorkman
If you want help, post an SSCCE with the most recent code, that way we're all looking at the same thing.
• August 6th, 2011, 09:35 AM
syang
```public class Un8 { public static void main( String [] args ) {   int [] even = new int [ 100 ]; int [] odd = new int [ 100 ];   int randomNum;   for( int count = 0 ; count < 2 ; count++ ) { System.out.println( count ); randomNum = (int) (25 * Math.random()); displayEvens( randomNum ); displayOdds( randomNum ); }   }   public static void displayOdds( int randomNum ) { for( int count = 0 ; count < 100 ; count++ ) { randomNum = 1+(int)(25*Math.random()); int randArr[m] = 1+(int)(25*Math.random()); // ^ Is this an array? My java instructor told me to use this to generate numbers greater than 3. System.out.println( randArr ); if(randomNum / 2 == 1) System.out.println( randomNum ); {     }   } }     public static void displayEvens( int randomNum ) { for( int count = 0 ; count < 100 ; count++ ) { randomNum = 1+(int)(25*Math.random()); int randArr[m] = 1+(int)(25*Math.random()); System.out.println( randArr ); if(randomNum / 2 == 0) System.out.println( randomNum ); {   }   } }   }```