-1

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 );
        }

    }
}
Sridhar
  • 11,466
  • 5
  • 39
  • 43
  • What is it doing (wrong) ? And what exactly do you want it to do differently? – Stultuske Sep 09 '14 at 11:20
  • Well, have you stepped through the code in your IDE's debugger? – OldProgrammer Sep 09 '14 at 11:23
  • in case we input a decimal or a any alphabet, instead of showing error the console keeps on doing this contionously "error "Please enter the number you want the table for: \n" – Harpreet Singh Saini Sep 09 '14 at 11:25
  • If you used a debugger, you could find out yourself. http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – Raedwald Sep 09 '14 at 12:08

2 Answers2

0

In addition to

    catch(InputMismatchException  e){
                System.out.println("Input Mismatch Error");
}

Add another after that:

    catch(Exception  e){
                System.out.println("Other Error");
}

Instead of input.nextLine(), use input.next()

Then check again

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

First,

input.nextInt()

doesn't capture newline. So you need to replace that with input.nextLine().

            int z = Integer.parseInt(input.nextLine());
            int y = Integer.parseInt(input.nextLine());

You can add an Exception catch block to handle any parsing errors. Making those changes to your code.

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 = Integer.parseInt(input.nextLine());
            System.out.println("Upto what number you want the table:\n");
            int y = Integer.parseInt(input.nextLine());
            Solve multiplyObj = new Solve();
            multiplyObj.multiply(z, y);
            g = 2;
        } catch (InputMismatchException e) {
            System.out.println("Error -- input again");
        }catch (Exception e) {
            System.out.println("Error -- input again");
        }           
    }while (g == 1);
    input.close();
}
Sridhar
  • 11,466
  • 5
  • 39
  • 43