**NOTE** Cannot use any utils like Hashset, scanner except JOptionPane, Arraylis
ID: 3546477 • Letter: #
Question
**NOTE** Cannot use any utils like Hashset, scanner except JOptionPane, Arraylist
This is my method for input so far.
The user must input 4 numbers those are: numerics only, range of 1 to 20 only, and cannot input same number.
Catching well for numerics only and ranges.
The problem is I need to validate that user cannot input the same number twice.
When it catches the duplicated number, it must go back to input prompt to retype as I did below for other validations.
private static int[] getInputR() {
int[] inputR = new int[4];
for (int i = 0; i < 4; i++) {
try {
inputR[i] = Integer.parseInt(JOptionPane
.showInputDialog("Type a regular lottery number "
+ (i + 1)));
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter the numeric Values Only!!!!");
i--;
}
if (inputR[i] < 0 || inputR[i] > 20) {
JOptionPane.showMessageDialog(null,
"ERROR! Enter the number between 1 to 20");
i--;
}
}
return inputR;
}
Explanation / Answer
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class TextFileExample {
private static int[] getInputR() {
int[] inputR = new int[4];
for (int i = 0; i < 4; i++) {
try {
inputR[i] = Integer.parseInt(JOptionPane.showInputDialog("Type a regular lottery number " + (i + 1)));
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "ERROR! Enter the numeric Values Only!!!!");
i--;
}
if (inputR[i] < 0 || inputR[i] > 20) {
JOptionPane.showMessageDialog(null, "ERROR! Enter the number between 1 to 20");
i--;
}
else{
for(int j = 0; j < i; j++){
if(inputR[j] == inputR[i]){
i--;
JOptionPane.showMessageDialog(null, "ERROR! Cannot enter number more than once!!!!");
continue;
}
}
}
}
return inputR;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.