I want the user to put only integers and if he enters anything else he should get my error and should be asked to input again. But this code is not working as I want my program to be. Please help me for this and other suggestion are also welcome.
//this is a separate main file
import java.util.*;
public class SolMul {
public static void main(String[] args) {
int g = 1;
Scanner input = new Scanner(System.in);
do{
try{
System.out.println("Please enter the number you want the table for: \n");
int z = input.nextInt();
System.out.println("Upto what number you want the table:\n");
int y = input.nextInt();
Solve multiplyObj = new Solve();
multiplyObj.multiply(z, y);
g = 2;
}catch(InputMismatchException e){
System.out.println("Error");
}
}
while(g==1);
input.close();
}
}
//this is a separate class file in a new window
public class Solve {
public void multiply(int number,int upto){
for (int x = 1; x <= upto; x ++){
System.out.printf("%d X %d = %d \n",number,x,number*x );
}
}
}