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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Writing a program with arrays and class methods... PLEASE HELP?

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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...

    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
    Last edited by syang; August 6th, 2011 at 09:28 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    syang (August 2nd, 2011)

  4. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Writing a program with arrays and class methods... PLEASE HELP?

    I added parentheses after it, but it still displays the error messages...

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing a program with arrays and class methods... PLEASE HELP?

    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.

  6. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by syang; August 2nd, 2011 at 03:16 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing a program with arrays and class methods... PLEASE HELP?

    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.

  8. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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>

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

  10. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    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++;
    }
     
    }
    }
     
    }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Writing a program with arrays and class methods... PLEASE HELP?

    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.

  12. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by syang; August 3rd, 2011 at 05:58 PM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    Last edited by Norm; August 3rd, 2011 at 05:35 PM.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?

  15. #14
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by syang; August 3rd, 2011 at 06:02 PM.

  16. #15
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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?

  17. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #17
    Junior Member
    Join Date
    Aug 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Writing a program with arrays and class methods... PLEASE HELP?

    My revised program...

    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 );
    {
     
    }
     
    }
    }
     
    }

  19. #18
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Question about writing a phonebook sorting program
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: March 25th, 2011, 09:25 AM
  2. Help requested - testing a class with a tester class, no methods allowed.
    By miketeezie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2011, 10:40 PM
  3. Help writing a Template Class
    By mamipapi in forum Object Oriented Programming
    Replies: 2
    Last Post: October 5th, 2010, 09:34 PM
  4. [SOLVED] Help with Class Methods(Probably simple)
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 9
    Last Post: May 27th, 2010, 09:53 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM