1

I am executing my code in java But I am getting number exception error everytime. Please help

class TestClass {
public static void main(String args[]) throws IOException {
  BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
  String input = inp.readLine();
  //Scanner sc = new Scanner(System.in);
  StringTokenizer stk = new StringTokenizer(input);
  int n = Integer.parseInt(stk.nextToken());
  int q = Integer.parseInt(stk.nextToken());
  int [] arr = new int[n];
  int [] st = new int [n];
  for(int i =0;i<n;i++){
    arr[i] = Integer.parseInt(stk.nextToken());
     st[i] = fib(arr[i]);

  }  
while(q>0){
    int l = Integer.parseInt(stk.nextToken());
    int r = Integer.parseInt(stk.nextToken());
     System.out.println(gcd(st,l,r));
    q--;
}

I am continuously getting error message:

Exception in thread "main" java.lang.NumberFormatException: For input string: "3 2" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at TestClass.main(Main.java:14)

4
  • What are your inputs and what is fib() and gcd()? Commented Jun 10, 2017 at 7:49
  • You are trying to parse number 3 2. This is the error, you can't parse a string with whitespaces. Try using TryParse method for avoid exceptions, but if you have wrong input values like 3 2 program are not going to print a number... Commented Jun 10, 2017 at 7:51
  • I can't see any way to get that particular error from the program you've shown here. Is there something you're not telling us? Commented Jun 10, 2017 at 7:57
  • Can you show the input you're feeding System.in? Commented Jun 10, 2017 at 8:00

1 Answer 1

0

Try to use split method instead:

Instead of:

StringTokenizer stk = new StringTokenizer(input);

int n = Integer.parseInt(stk.nextToken());

int q = Integer.parseInt(stk.nextToken());

Use:

String in[] = input.split(" ");

int n = Integer.parseInt(in[0]);

int q = Integer.parseInt(in[1]);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.