-
Question on Arrays
Hi guys, been a while...
anyways, i've decided to teach myself java, and in the direction book that I have, one of the suggested projects that I do is the following, and I was wondering how you guys would go about doing this:
Design and implement an application that reads an arbitrary number of integers that are in the range of 0 to 50 inclusive and counts how many occurrences of each are entered. After all input has been processed, print all of the values [with the number of occurances] that were entered one or more times.
Any Ideas? Thanks a lot, in advance.
-Michael Henk
[email protected]
-
Okay, i know how to create the array and get the users input for the number of entries and the values for the entries itself. I just need to know how to count the number of times each number appears in the array.
-
Ive been working on it. :) The code has a couple of quirks but it might give you a general direction to head in.
Code:
public class X{
public static void main(String[] args){
/*
for(int i = 0; i < args.length; ++i){
if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 49)
System.out.println("Values must be in the range of 0 to 49");
System.exit(0);
}
*/
int[] holder = new int[50];
for(int i = 0; i < args.length; ++i){
int value = Integer.parseInt(args[i]);
holder[value]++;
}
for(int i = 0; i < args.length; ++i){
if(i != 0){
if(Integer.parseInt(args[i]) == Integer.parseInt(args[i - 1])){
continue;
}else{
printOcc(args, holder, i);
}
}else{
printOcc(args, holder, i);
}
}
}
public static void printOcc(String[] args, int[] holder, int index){
int num = Integer.parseInt(args[index]);
System.out.println(args[index] + " appears " + holder[num] + " times ");
}
}
-
The following block works to limit the range of values the user enters but for some reason the array is somehow wiped clean afer the block executes thus if the user enters any values outside the indicated range an ArrayIndexOutOfBounds exception is thrown.
Code:
for(int i = 0; i < args.length; ++i){
if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 49)
System.out.println("Values must be in the range of 0 to 49");
System.exit(0);
}
-
It says 50 inclusive, so increase array size and upper bound of the check by one.
-
CornedBee. Any ideas to the problem i posted?
Code:
for(int i = 0; i < args.length; ++i){
if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 50)
System.out.println("Values must be in the range of 0 to 49");
System.exit(0);
}
-
I have no idea what you're talking about. To me it seems as if this program should simply exit if there is at least one argument passed. You forgot braces around the if block and the System.exit should therefore be called unconditionally.
Anyway...
Code:
import java.io.*;
class CountNumbers {
private static Reader in =
new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) {
int count = readNum();
int[] nums = new int[51];
while(count-- > 0) {
int t;
do {
t = readNum();
} while(t < 0 || t > 50);
++nums[t];
}
// Now all left to do is write out the array.
}
public static int readNum() {
StringBuffer buf = new StringBuffer();
char c;
while(Character.isWhitespace(c = in.read()))
;
while(!Character.isWhitespace(c)) {
buf.append(c);
c = in.read();
}
return Integer.parseInt(buf.toString());
}
}
-
Yeah sorry forgot to add in the brackets. I was rushing to get ready for a party so i slapped the code together in a rush.