1

my program is suppose to add 10 to an array of random numbers when the user enter an integer , the array of random numbers will be displayed and adds 10 to them under the first array and if the user doesn't enter an int then the try catch statement catches the error displaying an error message, so what i want to do is add a loop in the try catch statement that makes the user enter an int when they don't this is what i tried so far and didn't work

public class tryandcatch {

    public static void main(String[] args) {

        int[] tab=new int[10];
        int i;
        Scanner inp=new Scanner(System.in);
        while(true) {
        try{
        System.out.println("Please enter an integer number");
        i=inp.nextInt(); 


        for(i=0;i<tab.length;i++){
            tab[i]=((int)(Math.random()*100));
            System.out.print(tab[i]+" ");
        }
        addTen(tab);
        System.out.print("\n");

        for(i=0;i<tab.length;i++)System.out.print(tab[i]+" ");
        break;
        }

        catch(InputMismatchException e){
            System.out.println("The number must be integer");

            i=inp.nextInt(); 


        }

        }

    }


    static void addTen(int[] x){
        int i;
        for(i=0;i<x.length;i++) x[i]+=10;
    }


    }
Shubham
  • 549
  • 4
  • 11
samer g
  • 129
  • 6

3 Answers3

2
  1. Use Integer.parseInt(Scanner::nextLine()) instead of Scanner::nextInt(). Check this to learn more about it.
  2. For simplicity, you can use a boolean variable to track if loopback is required.

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] tab = new int[10];
        int n;
        Scanner inp = new Scanner(System.in);
        boolean valid;
        do {
            valid = true;
            try {
                System.out.print("Please enter an integer number: ");
                n = Integer.parseInt(inp.nextLine());
                for (int i = 0; i < tab.length; i++) {
                    tab[i] = ((int) (Math.random() * 100));
                    System.out.print(tab[i] + " ");
                }
                addTen(tab);
                System.out.println();
                for (int i = 0; i < tab.length; i++) {
                    System.out.print(tab[i] + " ");
                }
            } catch (NumberFormatException e) {
                System.out.println("The number must be integer");
                valid = false;
            }
        } while (!valid);
    }

    static void addTen(int[] tab) {
        for (int i = 0; i < tab.length; i++) {
            tab[i] += 10;
        }
    }
}

A sample run:

Please enter an integer number: a
The number must be integer
Please enter an integer number: 10.5
The number must be integer
Please enter an integer number: 5
21 50 83 72 95 60 61 64 98 95 
31 60 93 82 105 70 71 74 108 105 

Note that I have used do...while which guarantees that the code inside the loop block will be executed at least once. In this particular case, the use of do...while has also made the code easier to understand. However, the use of do...while to solve this problem is optional and you can continue using while instead of do...while if you wish so.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0

Use a break; statement.

This statement exits the loop, so enclose the try-catch statement in a while(True) loop. If an error is not raised, then the break; statement exits the loop.

PranavaGande
  • 791
  • 2
  • 16
0

Scanner tries to parse an input until it is successful. In your code it always tries to parse the first non-integer input again and again. In order to fix this you can read the user input without trying to parse it to an int. You could change your catch-block as follows:

      catch(InputMismatchException e){
        final String nonInteger = inp.next();
        System.out.println("The number must be integer. You typed: " + nonInteger);
      }
Michael Kreutz
  • 1,166
  • 3
  • 7