1
votes

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)

1
What are your inputs and what is fib() and gcd()? - Blasanka
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... - Dani
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? - Dawood ibn Kareem
Can you show the input you're feeding System.in? - SHG

1 Answers

0
votes

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