1

Hello everyone I'm trying to improve my Java skills by solving some problems from ACM, now the thing is my sample input looks like this :

 3 100
 34 100
 75 250
 27 2147483647
 101 304
 101 303
 -1 -1

So at first I'm just trying to read them but its not working here is the java code:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {

    public static void main(String args[]) {
        Scanner stdin = new Scanner(new BufferedInputStream(System.in));
        while (stdin.hasNext()) {
            System.out.println(stdin.nextInt() + " and the next " + stdin.nextInt());
        }
    }
}

I'm trying to send these inputs as an argument, and not by reading them from file, here is how:

alt text

The program just spins(executes) but not printing anything. How can I fix this?

3 Answers 3

5

The arguments you have specified in your wizard are not input to the program via std in. Instead, they are passed in in the String[] args arguments passed into your main method. As it looks as though you are running in Eclipse, you can go to the Console tab that gets generated and type into that window. Alternatively, you can iterate over the args param to get those values.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not using Eclipse, but this should print the numbers you have inputed. You are passing the numbers as string arguments, so this: "3 100" is actually equal to

String str = 3 + " " + 100;

This should print the numbers:

public static void main(String[] args) 
{
    for (String arg : args)
    {
        System.out.println(arg);
    }
}

Comments

0

Try this:

Scanner sc = new Scanner (System.in);
while (sc.hasNextInt()) {
    System.out.println(sc.nextInt());
}

After you start the program you should provide some numbers as an input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.