Thread: Need Java Help
-
March 7th, 2009, 01:24 PM #1Junior 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
-
March 8th, 2009, 03:27 AM #2
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:
To print numbers in this format:Code:for(int i = 0; i<numbers.length; i++) System.out.println(i + "\t \t" + numbers[ i ]);
1[12], 2[13], 3[14], 4[15], 5[16]
Change the println statement above to be
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.Code:if(i = 0) System.out.print(i + "["numbers[i] + "]"); else System.out.print(", " + i + "["numbers[i] + "]");
Your code seems fine. It compiles and runs, and does what you want it to. What is your question?
-
March 8th, 2009, 05:52 PM #3Junior 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) ;
-
March 8th, 2009, 08:08 PM #4
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
CreaturesCanon EOS 550D | Tamron 18 - 270mm 1:3.5 - 6.3 | Lensbaby Scout (Soft Focus Optic, Fisheye Optic) | Canon Speedlite 430EX II
-
March 8th, 2009, 10:29 PM #5Junior Member
- Join Date
- Mar 2009
- Posts
- 4
sorry if i am confusing.
When you compile the code you get something like this
See those number on the right is repeating...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...
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
-
March 11th, 2009, 03:20 PM #6Junior 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
-
Need Java Help!
By fhack2 in forum Webmastering and ProgrammingReplies: 2Last Post: March 26th, 2009, 09:09 PM -
Launching Java webpages with different Java settings?
By LostBok in forum Webmastering and ProgrammingReplies: 1Last Post: December 11th, 2006, 03:38 AM -
java hw help
By sr71000 in forum Webmastering and ProgrammingReplies: 2Last Post: November 2nd, 2005, 09:27 PM -
JAVA help?
By dchw_dude in forum Webmastering and ProgrammingReplies: 2Last Post: September 12th, 2005, 09:40 PM -
Java
By Rand Dusing in forum Webmastering and ProgrammingReplies: 3Last Post: August 17th, 2004, 07:42 AM



LinkBack URL
About LinkBacks



Reply With Quote

There is about 3cm space between the cards :)
Temps too high? ( GTX 580s )