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 6 of 6

Thread: lucky numbers

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default lucky numbers

    hi everyone, i have to make a java program that generates "lucky numbers" with arrays
    The finishing result should be this
    1 2 3 4 5 6 7 8 9 10 original
    1 3 5 7 9 remove every second number from the original
    1 3 7 9 remove every 3 number from the above
    1 3 7 remove every 4 number from above

    So far i have the first two but i'm stuck on the other the lines
    for( int j = 0 ; j < start.length; j +=2 ) {
     
    				System.out.print( start[i] + " ");

    this code is for the second line

    if anyone has any ideas they would be much appreciated


  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: lucky numbers

    I don't see any code that removes anything from an array, so I'm not sure how you plan to proceed from what you have. I believe you should rethink your approach.
    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. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky numbers

    i don't know how to remove something from an array, that the problem. And don't really see another approach

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: lucky numbers

    I am not certain if this is the best approach, but this is how I would go about it:

    1: Assign a variable some extreme value that it it is very unlikely that the User will ever use.. such as Integer.MAX_VALUE, to be the unsaid value. Thiis will avoid having to make a copy of the array.

    2: When 'removing' a value from the array, simply replace it's index with the previous variable.

    3: When printing the array, first check if the value is the same as the variable assigned in step 1, if it is then do not print it.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: lucky numbers

    i need to now how i can put the output from this code
    for( int j = 0 ; j < start.length; j +=2 ) {

    System.out.print( start[i] + " ");

    into a new for loop.

  6. #6
    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: lucky numbers

    Quote Originally Posted by robingeldolf View Post
    i need to now how i can put the output from this code
    for( int j = 0 ; j < start.length; j +=2 ) {

    System.out.print( start[i] + " ");

    into a new for loop.
    What do you mean into a new loop? A different loop? A nested loop? Something else?

    And for the record, how I would remove an element from an array is by creating a new array with a length one less than the current array, then copy over every value from the current array except the one you want to remove. You could optimize this if you figured out how many elements you would be removing beforehand.
    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. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. [SOLVED] Scale Numbers from 0.0f to 1.0f
    By techwiz24 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 09:50 PM
  3. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  4. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM