0

Following is the question for prime number generator problem (from spoj.com):

Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!

Input :

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output :

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example :

Input:

2

1 10

3 5

Output:

2

3

5

7


3

5 

Following is my code for the same:

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

Error: The code when submitted, produces RuntimeException on spoj.com, although it works completely fine on my system.

Jainam Jhaveri
  • 1,069
  • 14
  • 24

4 Answers4

1

Always handle the exception that can be raised (ideally, any exceptional behaviour that you can recover from, accoding to the Oracle documentation for Exception) and never consider user input as safe:

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" */
}
Giulio Biagini
  • 925
  • 5
  • 8
  • [Swallowing exceptions](http://en.wikipedia.org/wiki/Error_hiding) is not handling them. I'd rather get hit in my face with a srack trace than have the exception swallowed like in your code. *Do* sth with the exception. Log it at least. Don't swallow it. – Roadrunner Jan 22 '15 at 17:08
  • If exceptions are due to user input, as in this case, the best thing to do is `catch` them and print out a message for the user. – Giulio Biagini Jan 22 '15 at 17:14
  • @Roadrunner - the `ArrayIndexOutOfBoundsException` thrown by me, is handled by the last `catch`. That's because if numbers in input are greater than 2 or is only one, the printed out message, in my code, is always the same. This isn't *swallowing exceptions*. It's just a way to notice the user. – Giulio Biagini Jan 22 '15 at 17:26
1
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;
                    }
                }   
        }
    }
}   

//try this

  • what u were trying to do was store a array of string into another without its being declared properly. so what i did was.....declared the string array number in advance. Although i took 10 members it seems i need only a range so u can take 2 members in the string array so as to reduce space occupied during run. – Dipshil Agrawal Jan 22 '15 at 17:57
  • it has the same problem that I mentioned in my answer. The first line is an "empty" string that holds only the '\n' character. – Gabriel Espinel Jan 22 '15 at 18:25
0

It works for me. Print the error so we have more info. You can also do Scanner.nextInt(). Things like multiple spaces , tabs can mess stuff

VigneshM
  • 167
  • 1
  • 9
0

Are you using sc.nextInt() before the first sc.nextLine()? because if that's the case, you could have a '\n' character in the buffer after using it. So when you use nextLine() for the first time, you actually get the '\n' character instead of the next line. And when you try to parse to integer it fails.

See here Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

If this is the case, the solution is simple. Just fire a call sc.nextLine() that does nothing except to 'eat' that character from the buffer.

Community
  • 1
  • 1
Gabriel Espinel
  • 358
  • 2
  • 8