-3

Why am I getting a java.lang.numberformatexception in string??

BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
        System.out.println("Do you have any budget range?. If yes press y else press n");
        char ch1=(char)br.read();
        if(ch1=='y')
        {
            System.out.println("Please enter the lower limit and then the higher limit");
            int low_buj = Integer.parseInt(br.readLine());
            int high_buj = Integer.parseInt(br.readLine());
        }
        else
        {
            System.out.println("Oh okay");
        }
  • 2
    Replace `br.read()` with `br.readLine()` and check result using `equals("y")`. This is a `BufferedReader` variant of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/q/13102045/5221149). – Andreas Apr 03 '17 at 16:06
  • Please provide the input that you used. – Mason T. Apr 03 '17 at 16:07
  • WHatever you are reading just cannot be parsed as an Integer, just run a debugger on your code and check out what you are actually reading in. – Attila Repasi Apr 03 '17 at 16:28

1 Answers1

0

It is because of using br.read() before br.readLine(), so you always will have an empty string in each br.readLine() using.

Just make a little changes to make it working:

  BufferedReader br= new BufferedReader( new InputStreamReader(System.in));
    System.out.println("Do you have any budget range?. If yes press y else press n");
    String s1 = br.readLine();
    if("y".equals(s1))
    {
        System.out.println("Please enter the lower limit and then the higher limit");
        int low_buj = Integer.parseInt(br.readLine());
        int high_buj = Integer.parseInt(br.readLine());
    }
    else
    {
        System.out.println("Oh okay");
    }
}
eGoLai
  • 352
  • 2
  • 14