Reputation: 109
So i've got a problem in my java homework. The task is to write a program that reads in ten numbers and displays only distinct numbers along with the number of distinct values. what i've got so far is...
import java.util.Scanner;
import java.util.Collection;
public class Exercise06_05 {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int[] list = new int[10];//create my new 10 slot array
//i really want a variable length array but alas
for (int i = 0; i < 10; i++){//get and check the ten input variables
System.out.println("Enter an Integer");//ask for input
int integer = input.nextInt();//assign the input to a temp variable
if (isUnique(list, integer)){//check if the temp is unique to the array
list[i] = integer;//if so assign it
}
}
String output = "";
int j = 0;
for (j = 0; j < list.length; j++){
if(list[j] != 0){//this is where the error accours
output += (list[j] + " ");
}else
break;//this break ensures J doesn't get any higher
//so that i can plug that in for the number of distinct variables
}
System.out.println("The number of distinct numbers is " + j);
System.out.println(output);//print output, and the number of distinct values
}
public static boolean isUnique(int [] arry, int a){// my masterpiece of a method
for (int i = 0; i < (10);){
if (arry [i] == a){//check box
return false;//not unique
} else if (i == (arry.length - 1)){//we done yet?
return true;//if so, return that it's unique
}else//if we're not done increment the box
i++;//there that is
} return false;//now put this here just to safeguard
}
}
It works fine unless the user inputs two of the same ints in a row like 1 and then 1 again. What happens is the program doesn't store the second int, the array keeps a zero and then fails at the create output part. How do i get around this?
Upvotes: 1
Views: 17356
Reputation: 3767
Ok this is little different, it's not about answering your question, you already did that, it's about a bit more focus on your solution. You are reading the list of numbers first and then operating on those numbers. Here is another snippet:
loop = 0;
/**Start Reading your numbers from stdin or anything**/
while(There are numbers to read){
int number = read_number(), counter = 0, found = 0;
while(counter <= loop){
if(a[counter] == number){
found = 1; // you found a match for this number break here
break;
}
counter++;
}
if(found == 0){
/* We did not find our number in array*/
a[counter] = number;
}
}
That's it read only once and loop only once.
Upvotes: 2
Reputation: 27512
Well, for one thing, if the number you get is unique you insert it into the array. If it is not unique, you don't insert it into the array, but you still advance the array index. Thus, you're going to leave it with the initialization default value of zero.
There's two problems with this.
There's no way to distinguish "duplicate value" from "zero". If zero is not a legal value, then okay fine. If zero is legal, this won't work.
More fundamental, when you loop through the array on output, you quit when you hit the first zero. So if the user entered, say, 1,2,4,2,3,4, you're goint to fill your array with 1,2,4,0,3,0. Then when you display the output you're going to write 1,2,4, see the zero and quit, and declare that there are 3 unique values. You'll never reach the 3.
I don't know how much they've taught you about the available data structures yet. A better solution would be to use an ArrayList rather than an array, and then only add to the ArrayList when an incoming value is unique. If you haven't learned about that yet, another idea would be to keep a counter of the number of unique values as you go along. Don't insert in the array when you get a duplicate value, and also don't increment the counter. That is, have one counter that's the position in the arrray, which is the same as the number of unique values. Have another counter which is the number of input values read. This will be greater than the position of the array once you see the first duplicate.
On, on one detail point, not directly related to your question: In your isUnique function, why do you loop up to a hard-coded ten, and then have a separate IF statement to test the array length and break when you reach the end? It would be a lot simpler and easier to read if you coded:
public static boolean isUnique(int [] arry, int a)
{
for (int i = 0; i < arry.length)
{
if (arry [i] == a)
{
return false; //not unique
}
}
return true; // we made it through without finding a dup, must be unique
}
Or if, as I suggest above, you have a separate variable to say how much of the array is actually filled:
public static boolean isUnique(int [] arry, int filled, int a)
{
// filled should always be <=arry.length, but we can check both just to be safe
for (int i = 0; i < arry.length && i<filled)
{
if (arry [i] == a)
{
return false; //not unique
}
}
return true; // we made it through without finding a dup, must be unique
}
Upvotes: 0
Reputation: 137442
Even if the value not stored, i is increased by 1.
One solution is:
if (isUnique(list, integer)){//check if the temp is unique to the array
list[i] = integer;//if so assign it
}
else
{
list[i] = 0;
}
Which will store 0 in the cell, otherwise there is nothing (not even 0) in the cell, and the program crashes.
Instead, if you want to get another value to the cell, decrease i by one instead of assigning 0 to the cell.
Upvotes: 0
Reputation: 9018
Your problem is that you're trying to output more values than are in the array.
Doing int[] list = new int[10]
means that list.length will always be 10.
Either switch from an array to a List, or keep track of the number inserted.
Upvotes: 0