Java:在spoj.com上运行时出现RuntimeException

Java:在spoj.com上运行时出现RuntimeException,java,primes,runtimeexception,Java,Primes,Runtimeexception,以下是质数生成器问题(来自spoj.com): 彼得想为他的密码系统生成一些素数。救救他!您的任务是生成两个给定数字之间的所有素数 输入: 输入以单行中测试用例的数量t开始(t它对我有效。打印错误以便我们获得更多信息。 您还可以执行Scanner.nextInt()。 如多个空间,标签可能会混淆事物 始终< /强>处理可以引发的异常(理想情况下,您可以从异常中恢复的任何异常行为,以Oracle文档为例外)和绝对/强>考虑用户输入是安全的: BufferedReader br = new Buff

以下是质数生成器问题(来自spoj.com):

彼得想为他的密码系统生成一些素数。救救他!您的任务是生成两个给定数字之间的所有素数

输入:


输入以单行中测试用例的数量t开始(t它对我有效。打印错误以便我们获得更多信息。 您还可以执行Scanner.nextInt()。
如多个空间,标签可能会混淆事物

<强>始终< /强>处理可以引发的异常(理想情况下,您可以从异常中恢复的任何异常行为,以Oracle文档为例外)和<强>绝对/强>考虑用户输入是安全的:

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

int t = 2;

try {
    /* for each line */
    for (int i = 0; i < t; i++) {
        /* read the line */
        String line = br.readLine();

        /* split the line */
        String[] numbers = line.split(" ");
        if (numbers.length != 2)
            throw new ArrayIndexOutOfBoundsException();

        /* parse values */
        int min = Integer.parseInt(numbers[0]);
        int max = Integer.parseInt(numbers[1]);

        /* do your check */
        __find_prime_numbers__
    }
}
catch (NumberFormatException ex) {
    /* notice the user -> input format isn't correct, for example: "1 m" */
}
catch (ArrayIndexOutOfBoundsException ex) {
    /* notice the user -> input format isn't correct, for example: "1 " or "1 2 3" */
}
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int t=2;
试一试{
/*每行*/
for(int i=0;i输入格式不正确,例如:“1 m”*/
}
捕获(阵列索引边界外异常){
/*请注意用户->输入格式不正确,例如:“1”或“1 2 3”*/
}
包装abc;
导入java.util.Scanner;
课堂问题2{
公共静态void main(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int t=sc.nextInt();
int a、b、标志、计数;
字符串行[]=新字符串[t];
字符串[]编号=新字符串[10];

对于(int i=0;i您是否在第一个
sc.nextLine()
之前使用了
sc.nextLine()
?因为如果是这种情况,您可以在使用后在缓冲区中有一个
'\n'
字符。所以当您使用
nextLine()时
这是第一次,实际上您得到的是
'\n'
字符,而不是下一行。当您尝试解析为整数时,它失败了

看这里


如果是这种情况,解决方案很简单。只需启动一个调用,
sc.nextLine()
,该调用除了从缓冲区中“吃掉”该字符外,什么都不做。

会出现哪一个错误?线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“……这是自定义的……您认为会从中弹出什么数字?”?尝试“\s+”或使用trim()在你分裂之前,我应该把完整的代码作为参考吗?不是在处理它们。我宁愿被一个srack跟踪击中我的脸,也不愿让异常像你的代码一样被吞没。用异常做某事。至少记录它。不要吞没它。如果异常是由于用户输入造成的,就像在这种情况下,最好的做法是
catch
t哼哼并为用户打印一条消息。@Roadrunner-我抛出的
ArrayIndexOutOfBoundsException
由最后一个
catch
处理。这是因为如果输入中的数字大于2或只有一个,打印出来的消息在我的代码中总是一样的。这不是吞咽异常。这只是一种注意到e user.你试图做的是将一个字符串数组存储到另一个数组中,而不正确声明它。因此我所做的是….提前声明字符串数组号。虽然我使用了10个成员,但我似乎只需要一个范围,这样你就可以在字符串数组中使用2个成员,以减少运行期间占用的空间。它与我遇到的问题相同在我的回答中提到。第一行是一个“空”字符串,只包含“\n”字符。
2

3

5

7


3

5 
package competitivecoding;

import java.util.Scanner;

class problem2{
public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    Scanner st = new Scanner(System.in);        
    int t = sc.nextInt();   // inputs the "no." of lines that users want to enter
    int a,b, flag, count;
    String line[] = new String[t];
    String[] number=new String[2];

    for(int i=0; i<t; i++){
        line[i] =st.nextLine();
    }

    for(count=0; count<t; count++){
        number  = line[count].split(" ");

        a = Integer.parseInt(number[0]);
        b = Integer.parseInt(number[1]);

        for(int i=a; i<=b; i++){
            for(int j=2; j<=i; j++){
                if(i%j==0){
                    if(i==j)
                       System.out.println(i);
                    else break;
                }
            }
        }

        System.out.println();
    }
}
}   
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int t = 2;

try {
    /* for each line */
    for (int i = 0; i < t; i++) {
        /* read the line */
        String line = br.readLine();

        /* split the line */
        String[] numbers = line.split(" ");
        if (numbers.length != 2)
            throw new ArrayIndexOutOfBoundsException();

        /* parse values */
        int min = Integer.parseInt(numbers[0]);
        int max = Integer.parseInt(numbers[1]);

        /* do your check */
        __find_prime_numbers__
    }
}
catch (NumberFormatException ex) {
    /* notice the user -> input format isn't correct, for example: "1 m" */
}
catch (ArrayIndexOutOfBoundsException ex) {
    /* notice the user -> input format isn't correct, for example: "1 " or "1 2 3" */
}
package abc;

import java.util.Scanner;

class problem2{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        int a,b, flag, count;
        String line[] = new String[t];
        String[] number=new String[10];

        for(int i=0; i<t; i++){
            line[i] =sc.nextLine();
        }
        for(count=0; count<t; count++){
            number = line[count].split(" ");}
            a = Integer.parseInt(number[0]);
            b = Integer.parseInt(number[1]);

            for(int i=a; i<=b; i++){
                for(int j=2; j<=i; j++){
                    if(i%j==0){
                        if(i==j)
                           System.out.println(i);
                        else break;
                    }
                }   
        }
    }
}