简体   繁体   English

字符串令牌生成器在使用BufferedReader获取空间分隔值时给出异常

[英]String Tokenizer giving exception while taking space saperated values using BufferedReader

I tried to start using BufferedReader instead of Scanner. 我试图开始使用BufferedReader而不是Scanner。 While coding for a question on codechef (SMRSTR), I tried taking space separated inputs by using StringTokenizer but it is raising exception ie NumberFormatException . 在就代码厨师(SMRSTR)的问题进行编码时,我尝试使用StringTokenizer接受空格分隔的输入,但它引发了异常,即NumberFormatException I found some question on StackOverflow regarding it but I think my problem is different, so I posted one. 我在StackOverflow上发现了一个与此有关的问题,但我认为我的问题有所不同,因此我发布了一个问题。
Input: 1 输入1
2 3 2 3
2 3 2 3
5 100 8 5 100 8

I am getting: 我正进入(状态:

Exception in thread "main" java.lang.NumberFormatException: For input string: "2 3" 线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:“ 2 3”
at java.lang.NumberFormatException.forInputString(Unknown Source) 在java.lang.NumberFormatException.forInputString(未知来源)
at java.lang.Integer.parseInt(Unknown Source) 在java.lang.Integer.parseInt(未知来源)
at java.lang.Integer.parseInt(Unknown Source) 在java.lang.Integer.parseInt(未知来源)
at A.main(A.java:11) 在A.main(A.java:11)

I am getting first input t correctly from br.readLine(); 我从br.readLine();得到正确的第一个输入t br.readLine(); But next inputs n,q are giving the mentioned exception. 但是接下来的输入n,q给出了所提到的异常。 I think the problem is in the nextToken from StringTokenizer, but still not getting it clearly. 我认为问题出在StringTokenizer的nextToken中,但仍然不清楚。

Here is the code: 这是代码:

import java.io.*;
import java.util.*;

 class A{
public static void main(String arg[]) throws IOException
{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer s = new StringTokenizer(br.readLine());

    int t= Integer.parseInt(br.readLine());
    while(t-->0)
    {
        int n,q,i;
        n=Integer.parseInt(s.nextToken());
        q=Integer.parseInt(s.nextToken());
        int D[]= new int[n];
        int Q[]=new int[q];
        long x=1;
        for(i=0;i<n;i++)
        {
            D[i]=Integer.parseInt(s.nextToken());
            x=x*D[i];
        }
        for(i=0;i<q;i++)
        {
            Q[i]=Integer.parseInt(s.nextToken());
            if(x>1000000000)
                Q[i]=0;
            else
            Q[i]=(int)(Q[i]/x);
        }
        for(i=0;i<q;i++)
            System.out.print(Q[i]+" ");
    System.out.println("");
    }
}
}

Assuming your first line is a single number and your second line a string of space separated numbers (if not, edit your question with your actual input) 假设第一行是一个数字,第二行是一个由空格分隔的数字字符串(如果不是,请使用实际输入内容来编辑问题)

I think you want to read t this way: 我想你想读t这种方式:

int t = Integer.parseInt(s.nextToken());

Then read your next line into your tokenizer 然后将下一行读入令牌生成器

s = new StringTokenizer(br.readLine());

The code before the while loop should be: while循环之前的代码应为:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer s = new StringTokenizer(br.readLine());

int t = Integer.parseInt(s.nextToken());
s = new StringTokenizer(br.readLine());

EDIT 编辑

You need to read each line in the tokenizer before using the next Int method. 在使用下一个Int方法之前,您需要阅读令牌化程序中的每一行。 This should work. 这应该工作。

Input: 输入:

1 
2 3  
2 3 
5 100 8

Output: 输出:

0 16 1 

Working code: 工作代码:

public static void main(String arg[]) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    // read first line in tokenizer
    StringTokenizer s = new StringTokenizer(br.readLine());
    //parse t
    int t = Integer.parseInt(s.nextToken());

    // read second line in tokenizer
    s = new StringTokenizer(br.readLine());

    while(t-->0) {
        int n,q;
        // parse n and q (2, 3)
        n=Integer.parseInt(s.nextToken());
        q=Integer.parseInt(s.nextToken());

        int D[]= new int[n];
        int Q[]=new int[q];
        long x=1;
        // read third line in tokenizer
        s = new StringTokenizer(br.readLine());
        for(int i=0;i<n;i++) {
            D[i]=Integer.parseInt(s.nextToken());
            x=x*D[i];
        }
        // read fourth line in tokenizer
        s = new StringTokenizer(br.readLine());
        for(int i=0;i<q;i++) {
            Q[i]=Integer.parseInt(s.nextToken());
            if(x>1000000000)
                Q[i]=0;
            else
                Q[i]=(int)(Q[i]/x);
        }
        for(int i=0;i<q;i++)
            System.out.print(Q[i]+" ");

        System.out.println("");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM