线程" main"中的例外情况java.lang.NumberFormatException:

时间:2017-06-10 07:41:00

标签: java parsing exception bufferedreader parseint

我在java中执行我的代码但是我每次都会收到数字异常错误。请帮忙

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--;
}

我不断收到错误消息:

  

线程中的异常&#34; main&#34; java.lang.NumberFormatException:用于输入   string:&#34; 3 2&#34;在   java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)     在java.lang.Integer.parseInt(Integer.java:580)at   java.lang.Integer.parseInt(Integer.java:615)at   TestClass.main(Main.java:14)

1 个答案:

答案 0 :(得分:0)

尝试使用拆分方法:

而不是:

StringTokenizer stk = new StringTokenizer(input);

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

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

使用:

[] = input.split中的字符串(&#34;&#34;);

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

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