Hi
Here's a console program that solves the problem.
Input is evaluated to check if the user has typed in an integers and it it's unique.
The unique check is done by comparing the new integers with the previous accepted integers.
/* Input5uniquenumbers.java
* Created on 22. marts 2008, 23:56
*/
import java.io.*;
public class Input5uniquenumbers {
public static void main(String[] args) {
int numberOfInputs = 5;
System.out.println( "\nPlease type in " + numberOfInputs + " integers: " );
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader stdin = new BufferedReader( isr );
String rawInput;
int inputAsInteger = -1;
int[] goodInteger = new int[numberOfInputs +1];
goodInteger[0] = -1; // index 0 is ignored
boolean inputOk;
boolean isUnique;
//
for ( int intNum = 1; intNum <= numberOfInputs; intNum++) {
inputOk = true;
rawInput = null;
System.out.print( "\n" + intNum + ". integer ..");
try {
rawInput = stdin.readLine();
inputAsInteger = Integer.parseInt( rawInput.trim() );
} catch (IOException ioError) {
inputOk = false;
System.err.println( ioError );
} catch (NumberFormatException ioError ) {
inputOk = false;
System.err.println( "Not an integer" );
}
// Evaluation
if ( inputOk ) {
// is the number unique ?
isUnique = true;
// compares the previous accepted integers with the new one
for(int ix = 1; ix < intNum; ix++) {
if ( goodInteger[ix] == inputAsInteger ) {
isUnique = false;
System.err.println( "Not unique" );
}
}
if ( isUnique ) {
goodInteger[intNum] = inputAsInteger;
} else {
intNum--; // eliminate the for loops intNum++
}
} else {
intNum--;
}
}//ends for loop
System.out.print( "\nYou typed in the following unique integers: " + goodInteger[1]);
for(int ix = 2; ix <= numberOfInputs; ix++) {
System.out.print( ", " + goodInteger[ix] );
}
}//ends main()
}
Enter your message below
Sign in or Join us (it's free).