Results 1 to 5 of 5

Thread: Please help me!

  1. #1
    gg2901 is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Question Please help me!

    I'm new java programming. :confused: So I really need some help. I'm trying to create a program that generates 5 random numbers between 1-9. I got it to do that, but now I need to generate a new number for any duplicate numbers. I've done everything, Its probably really easy, but I dont know how to do it.


    import javax.swing.*;
    import java.util.Arrays;

    public class CodeGenerator
    {
    String numberStr;
    int number;
    final int ARRAY_SIZE = 5;
    int[] numbers = new int[ARRAY_SIZE];
    java.util.Random rand = new java.util.Random();


    //Constructor
    public CodeGenerator()
    {
    calculate();
    }



    public void calculate()
    {

    for(int index=0; index < numbers.length; index++)
    {
    numbers[index] = (int)(Math.random()*10);

    while (numbers[index] == 0 || numbers[index] == 10)
    {
    numbers[index] = (int)(Math.random()*10);
    }

    }

    numberStr = Arrays.toString(numbers);
    numberStr = numberStr.trim();
    numberStr = numberStr.replace("[" , "");
    numberStr = numberStr.replace("]", "");
    numberStr = numberStr.replace(",", "");
    numberStr = numberStr.replace(" ", "");

    }
    public String getNumString()
    {
    calculate();
    return numberStr;
    }

    public int getNumNumber()
    {
    calculate();
    number = Integer.parseInt(numberStr);
    return number;

    }

    }

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    here's a little pseudo code for the logic
    Java Code:
    LOOP
    {
      LET r = get_random_number();
      boolean b = check_for_dups( r );
      if b is false{ // meaning no dups
        array.add( r );
        index++;
      }
      else loop again with out increasing index
    }
    ...
    boolean check_for_dups(VAR r){
      for each array element
        does r == element ?
        if(yes) return true;
        else continue;
    
      if nothing is found, then return false
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    suppose you have an array(global declared/field member) of random(unique numbers from 1-9)

    You may create a method that checks the number's existency in the array, (returns true or false)

    From that, you may do a while loop like:
    Java Code:
    array[];
    while( array.stored < 5 ){
           generated = generateRandom(1-9);
           if( generated not exists in array ){
                 store generated to array;
                 array.stored++;
           }
    }
    take advantage of ArrayList class.
    please do correct me if it doesn't make any sense,
    Last edited by sukatoa; 06-28-2009 at 08:36 AM.
    freedom exists in the world of ideas

  4. #4
    gg2901 is offline Member
    Join Date
    Jun 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks so much. I finally got it to work using an array list and using the shuffle method . But now I just need some help conveting my array list to a string? Is there a way to do that? Thats the only thing I need and Im done! =(

    import javax.swing.*;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.ArrayList;

    public class CodeGenerator
    {

    String numberStr;
    int number;
    java.util.Random rand = new java.util.Random();
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    int arrayList2;


    //Constructor
    public CodeGenerator()
    {
    calculate();
    }



    public void calculate()
    {

    for(int index = 0; index < 9; index++)
    {
    numbers.add(index+1); //Adding to the ArrayList
    }
    Collections.shuffle(numbers); //shuffling all 9 numbers



    for(int index2 =0; index2 < 5; index2++)
    {
    arrayList2 = numbers.get(index2));
    System.out.print(arrayList2 + " ");
    }


    //numberStr = ArrayList.toString(numbers);
    //numberStr = numberStr.trim();
    //numberStr = numberStr.replace("[" , "");
    //numberStr = numberStr.replace("]", "");
    //numberStr = numberStr.replace(",", "");
    //numberStr = numberStr.replace(" ", "");

    }


    public String getNumString()
    {
    calculate();
    return numberStr;
    }

    public int getNumNumber()
    {
    calculate();
    number = Integer.parseInt(numberStr);
    return number;

    }

    }
    Last edited by gg2901; 06-28-2009 at 11:52 PM. Reason: I accidentially pressed enter before I was done with the reply.

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    try something like this...

    Java Code:
    //pseudocode
    create string;
    get Iterator for ArrayList (ArrayList.iterator() returns it)
    while(the iterator has more elements to iterate through)
    add element to string;
    
    return string
    Also, use code tags ([code] and [/code]) or # button in advanced mode.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •