Results 1 to 2 of 2
- 02-24-2009, 03:17 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
[SOLVED] need advice on this method.
For example, consider this array have 10 elements: 65, 50, 79, 58, 83, 91, 65, 98, 79, 79
this program outputs this
The number of times that 65 appears in the array: 2
Indices that 65 is located: 0 6
The number of times that 65 appears in the array: 2
Indices that 65 is located: 0 6
The number of times that 79 appears in the array: 3
Indices that 79 is located: 2 8 9
The number of times that 79 appears in the array: 3
Indices that 79 is located: 2 8 9
can someone help me on what i need to change to get the program to display only one once for the same number?:confused::confused:
Java Code:import java.util.Scanner; public class RedundantElements { public static void main (String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter an integer for n, ranging 10 and 30."); int inp = input.nextInt(); int [] array = new int[inp]; int [] newArr = assign(array); System.out.println("The random values in an array with size "+array.length+" are:"); displaynum(newArr); displayVaFq(newArr); } //assign random number to n size array public static int[] assign(int[] array) { int [] newArr = new int[array.length]; for (int i =0; i<array.length; i++) { newArr[i] = (int)(Math.random()*51)+50; } return newArr; }//end of assign method //display numbers public static void displaynum(int[] array) { for (int j=0; j< array.length; j++) { if ((j+1)%5==0) System.out.println(array[j]+" "); else System.out.print(array[j]+" "); }//end of for }//end of void method //display value and fequency [COLOR="SeaGreen"] public static void displayVaFq(int[] array) { int count = 1; int rep =0; for (int k=0; k<array.length; k++) { for (int L=0; L<array.length; L++) { if (array[k] == array[L] && k != L) { count++; rep = k; }//end of if }//end of second for if (count >= 2) { System.out.println("\nThe number of times that "+ array[rep] + " appears in the array: "+count); System.out.println("Indices that "+array[rep]+" is located: "); for (int i = 0; i<array.length; i++) { if (array[rep] == array[i] ) { System.out.print(" "+i+" "); } } }//end of if //clear count and rep count = 1; rep = 0; }//end of first for[/COLOR] }//end of displayVaFq method }
- 02-24-2009, 06:22 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
Need advice on JSP with bean
By butterhero in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-18-2009, 11:40 AM -
JTextField add numbers advice
By thelinuxguy in forum Advanced JavaReplies: 9Last Post: 02-20-2009, 02:03 AM -
Some advice please!
By awebbtt in forum New To JavaReplies: 3Last Post: 02-02-2009, 07:23 PM -
Advice needed
By dineshjf in forum Advanced JavaReplies: 18Last Post: 01-09-2009, 03:17 AM -
Advice on best method for....
By shaungoater in forum Java 2DReplies: 1Last Post: 06-23-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks