How to use BufferedReader to get an interger

Viewed 64

I'm writing a little program for myself and I bump into a problem.

I want to be able to type a number into the console, and extract the number for a variable. Until this point I got it.

int aNumber = Integer.parseInt(br.readLine());

Except I want to get an error when there is something entered into the console that's not a number.

Something like this but working ofcourse:

void Test() {
    while (true) {
        try {
            aNumber = Integer.parseInt(br.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (aNumber >= 1) {
            System.out.println("Hurray you enterd a number! It is " + aNumber + ".");
            return;
        } else {
            System.out.println("Error: Something went wrong. Please try again.\n");
        }
    }
}

If you enter a number it works fine. But I want to be able to enter texts and getting an error to try again.

Example if you type in the console:

1

Answer:

Hurray you enterd a number! It is 1.

=====

2564

Answer:

Hurray you enterd a number! It is 2564.

=====

eaf123

Answer:

Error: Something went wrong. Please try again.

=====

Thanks for everyone trying to help.

Lars Meeuwsen

3 Answers

This will work. I hope you understand the solution. Integer.parseInt() throws NumberFormatException if parse fails.

void test() {
            while (true) {
                int aNumber;
                try {
                    aNumber = Integer.parseInt(br.readLine());
                    System.out.println("Hurray you enterd a number! It is " + aNumber + ".");
                    return;
                } catch (NumberFormatException | IOException e) {
                    System.out.println("Error: Something went wrong. Please try again.\n");

                }
            }
        }

Your code is not working because entering a string or any other type besides int, generates a NumberFormatException and you are not catching it.

The if statement is unnecessary since the catch block will be executed only if text is given. Exception will be thrown at conversion so the line below will not execute.

Please check this code

void Test() {
    while (true) {
        try {
            aNumber = Integer.parseInt(br.readLine());
            System.out.println("Hurray you enterd a number! It is " + aNumber + ".");
            return;
        } catch (NumberFormatException | IOException e) {
            e.printStackTrace();
            System.out.println("Error: Something went wrong. Please try again.\n");
        }
    }
}

the simplest thing you could do is to catch the NumberFormatException and then print the message you want.

See below for a working example:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadNumber {

    public static void main(String[] args) {
        int aNumber = 0;

        while (true) {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                aNumber = Integer.parseInt(br.readLine());
            } catch (NumberFormatException e) {
                System.out.println("That wasn't a number.");
                continue;
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (aNumber >= 1) {
                System.out.println("Hurray you entered a number! It is " + aNumber + ".");
                continue;
            } else {
                System.out.println("Error: Something went wrong. Please try again.\n");
            }
        }
    }
}
Related