Code:
public class CountAdder {
public CountAdder() {
}
/**
* Get user integer input, don't stop until it's correct
* @param prompt
* @return
*/
private int getUserInt(String prompt) {
while (true) {
String s = JOptionPane.showInputDialog(prompt);
try {
return Integer.parseInt(s);
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, s + " is not an integer",
"Input error", JOptionPane.ERROR_MESSAGE);
}
}
}
public void doMultiplication() {
int x=getUserInt("Enter an Integer");
int y=getUserInt("Enter a second Integer");
int total=0;
for (int i=0; i<y; i++) {
total += x;
}
System.out.println(total);
}
/**
* Make an instance of class, then invoke the instance method(s).
* @param args
*/
public static void main(String[] args) {
CountAdder cA = new CountAdder();
cA.doMultiplication();
}
}
Bookmarks