Writing a program with arrays and class methods... PLEASE HELP?
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???
Thank you all in advance1
Re: Writing a program with arrays and class methods... PLEASE HELP?
Math.random() is a method, not a variable, so it requires parenthesis after it. PS- You forgot the highlight tags.
Re: Writing a program with arrays and class methods... PLEASE HELP?
I added parentheses after it, but it still displays the error messages...
Re: Writing a program with arrays and class methods... PLEASE HELP?
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.
Re: Writing a program with arrays and class methods... PLEASE HELP?
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?
Re: Writing a program with arrays and class methods... PLEASE HELP?
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.
Re: Writing a program with arrays and class methods... PLEASE HELP?
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
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>
Re: Writing a program with arrays and class methods... PLEASE HELP?
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.
Re: Writing a program with arrays and class methods... PLEASE HELP?
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++;
}
}
}
}
Re: Writing a program with arrays and class methods... PLEASE HELP?
Quote:
it gives me unlimited numbers
Please explain? Or copy and paste some of the console here with comments about the problem.
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.
Re: Writing a program with arrays and class methods... PLEASE HELP?
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.
Re: Writing a program with arrays and class methods... PLEASE HELP?
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
Re: Writing a program with arrays and class methods... PLEASE HELP?
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?
Re: Writing a program with arrays and class methods... PLEASE HELP?
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
Re: Writing a program with arrays and class methods... PLEASE HELP?
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?
Re: Writing a program with arrays and class methods... PLEASE HELP?
If you want help, post an SSCCE with the most recent code, that way we're all looking at the same thing.
Re: Writing a program with arrays and class methods... PLEASE HELP?
My revised program...
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 < 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 );
{
}
}
}
}
Re: Writing a program with arrays and class methods... PLEASE HELP?
Do you really not use indentation?
That code contains compiler errors, so it can't be what you're running.