I am writing a simple program that takes multiple inputs and displays the largest then second largest. My only problem is that I want the program to accept only single digi
For this type of problem, personally, I would use a DocumentFilter
It allows you to restrict the type of characters coming into the field as well as the number of characters.
public class RestrictInput {
public static void main(String[] args) {
new RestrictInput();
}
public RestrictInput() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField(2);
field.setHorizontalAlignment(JTextField.RIGHT);
((AbstractDocument) field.getDocument()).setDocumentFilter(new RestrictFilter());
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(new JLabel("Please enter a integer:"), gbc);
gbc.gridx++;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
panel.add(field, gbc);
int result = JOptionPane.showConfirmDialog(null, panel, "Input", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("Use entered " + field.getText());
}
}
});
}
public class RestrictFilter extends DocumentFilter {
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
String currentText = fb.getDocument().getText(0, fb.getDocument().getLength());
if (currentText.startsWith("-") || text.equals("-") || fb.getDocument().getLength() < 1) {
String value = text.substring(0, 1);
if (value.equals("-")) {
if (currentText.startsWith("-")) {
super.remove(fb, 0, 1);
} else {
super.insertString(fb, 0, value, attr);
}
} else if (fb.getDocument().getLength() < 2 && (value.equals("-") || Character.isDigit(value.charAt(0)))) {
super.insertString(fb, offset, value, attr);
}
}
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String string, AttributeSet attr) throws BadLocationException {
if (length > 0) {
fb.remove(offset, length);
}
insertString(fb, offset, string, attr);
}
}
}
Check out MDP's Weblog and Text Component Features, in particular the section of Implementing a Document Filter
Just use a custom JOptionDialog where you restricted the JTextField to 1 char width.
//Set the counter
counter++;
while (true) {
//Input integer, set the largest number as the first output
number = Integer.parseInt(JOptionPane.showInputDialog("Enter integer"));
if (number < 10 && number > -10) break; //If it's one digit, it's OK
JOptionPane.showMessageDialog(null, "Enter only one digit",
"Too many digits", JOptionPane.ERROR_MESSAGE);
}
What this does is start an infinite loop. If the number is only one digit, it will end the loop and continue on. Otherwise, it will start from the beginning of the loop again and ask for a valid number.