Results 1 to 4 of 4
Thread: Same values in an array
- 02-24-2009, 07:01 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
-
- 02-24-2009, 07:54 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 21
- Rep Power
- 0
Hi all. I worked the following out. It works if there are two values. Like if i enter 2,3,3,4,5 or 5,6,7,8,6. But it wont work with 4,5,4,6,4. Any ideas?
Java Code:class Array { public static void main (String [] args) { int [] array = new int[5]; int count = 0; System.out.println("Please enter five numbers:"); for (int i = 0; i < array.length; i ++) { array[i] = Console.readInt(); } System.out.println(); System.out.println(); for (int i = 0; i < array.length; i ++) { for (int j = 1; j < array.length; j++) { if (array[i] == array[j] && j != i) count++; } } System.out.println(); System.out.println(count); }// main }// class
- 02-24-2009, 08:33 PM #4
its better to say:
for (int j = i+1; j < array.length; j++)
edit:
and i would zero out the matched numbers. (or make it -1 or whatever)
if (array[i] == array[j] && array[j] != 0)
count++;
array[j] = 0;
}Last edited by angryboy; 02-24-2009 at 08:37 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
3d array filled with garbage values
By jon80 in forum New To JavaReplies: 5Last Post: 01-01-2009, 10:12 PM -
Array Assign Values from a Textfile
By fawadafr in forum Java AppletsReplies: 6Last Post: 11-30-2008, 12:10 AM -
writing array values to another class
By ronald christian in forum New To JavaReplies: 27Last Post: 11-07-2008, 04:08 PM -
replacing array values
By Jononomous in forum New To JavaReplies: 1Last Post: 05-22-2008, 03:27 PM -
[SOLVED] How to read a file and compare Array values
By DonCash in forum Advanced JavaReplies: 2Last Post: 04-02-2008, 02:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks