+ Reply to Thread
Results 1 to 6 of 6

Thread: Need Java Help

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    4

    Need Java Help

     
    Hi
    I have this code that creates an array of 100 random integers that are in range of 0 - 25
    Code:
    import java.util.* ;
    
    public class Int 
    {
       public static void main( String args[] )
       {
          int[] numbers = new int[100] ;
          String output = "";
          output += "Subscript\t\tIntegers\n";
    
          Random generator = new Random();
          
          for (int i = 0; i < 100 ; i++)
             numbers[i] = generator.nextInt(26) ;
             
       
          for ( int i = 0; i < numbers.length; i++ ) 
             output += i + "\t \t" + numbers[ i ] + "\n";
          System.out.print(output) ;
      
        }
    }

    i am trying to create a code that will print all the values that appears in the array one or more times. I am not sure what to do as this is my first time trying to make code like this.
    If anyone can help i would be greatly appreciate..
    Thanks

  2. #2
    Ultimate Member dchw_dude's Avatar
    Join Date
    Nov 2004
    Location
    Provo, UT
    Posts
    1,356
    At first glance your code looks fine. IMO your output string will look a little funny, I would write it like this:

    To print numbers in this format:
    1 12
    2 13
    3 14
    4 15
    5 16

    do this:
    Code:
    for(int i = 0; i<numbers.length; i++)
         System.out.println(i + "\t \t" + numbers[ i ]);
    To print numbers in this format:

    1[12], 2[13], 3[14], 4[15], 5[16]

    Change the println statement above to be
    Code:
    if(i = 0)
         System.out.print(i + "["numbers[i] + "]");
    else
         System.out.print(", " + i + "["numbers[i] + "]");
    And importing java.util.* is a bit much. Since you're not using anything else, just importing java.util.Random would be the best practice here.

    Your code seems fine. It compiles and runs, and does what you want it to. What is your question?

    TechIMO Folding@home Team #111 - Crunching for the cure!

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    4
    thanks for the reply;
    i am trying to print all the values that appears in the array one or more times but i am not sure how to.
    I was thinking about making an "if/else" statement to do that but had no luck
    can i do something like this :
    Code:
     if (sum = (number[i] == number[i]))
              System.out.print("number of integers repeated:" + sum);
              else
              System.out.print(output) ;

  4. #4
    Go back to sleep Creatures's Avatar
    Join Date
    Jul 2002
    Location
    Switzerland
    Posts
    9,278
    number[i] is always equal number[i], i dont seem to understand why you want to check that?
    could you specify what you want? because i dont get it

    Creatures
    Canon EOS 550D | Tamron 18 - 270mm 1:3.5 - 6.3 | Lensbaby Scout (Soft Focus Optic, Fisheye Optic) | Canon Speedlite 430EX II

  5. #5
    Junior Member
    Join Date
    Mar 2009
    Posts
    4
    sorry if i am confusing.
    When you compile the code you get something like this
    1 2
    2 11
    3 2
    4 17
    5 1
    6 4
    7 14
    8 22
    9 15
    10 12
    11 16
    12 0
    13 1
    14 20
    15 20
    16 12
    17 1
    etc...
    See those number on the right is repeating...
    i want to create a code that will tell which number are repeating.... but i am not sure how i can do that, so if you can tell me it would be great.
    Thanks

  6. #6
    Junior Member
    Join Date
    Mar 2009
    Posts
    4
    i just realize that i have to create a count method, anyone can tell me how can do that.
    thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need Java Help!
    By fhack2 in forum Webmastering and Programming
    Replies: 2
    Last Post: March 26th, 2009, 09:09 PM
  2. Launching Java webpages with different Java settings?
    By LostBok in forum Webmastering and Programming
    Replies: 1
    Last Post: December 11th, 2006, 03:38 AM
  3. java hw help
    By sr71000 in forum Webmastering and Programming
    Replies: 2
    Last Post: November 2nd, 2005, 09:27 PM
  4. JAVA help?
    By dchw_dude in forum Webmastering and Programming
    Replies: 2
    Last Post: September 12th, 2005, 09:40 PM
  5. Java
    By Rand Dusing in forum Webmastering and Programming
    Replies: 3
    Last Post: August 17th, 2004, 07:42 AM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews