To use Java in Eclipse
- Create a Java Project (File → New → Java Project) with the same name as the problem (Practice).
- Create one (and only one) Java class (File → New → Class) named for the problem (Practice.java).
- Create a text file (File → New → File) named input and put it in the src folder.
- Write your code and a main method.
- Use the Scanner class for input and System.out.println() for output.
To run while reading input from the prompt
- Click on the Java file or the project and then click on the Run Tools button (the black tab next to
the green run arrow with the red square). A pulldown menu with ACM run options for each problem will appear.
- If you see an option which matches your problem (ACM_Practice), click it.
- If not, click on External Tools Configurations and then select your problem from the Programs list
Click on run. From now on, this option will appear under the Run Tools button.
- You will see a compilation and run message appear in the console window.
Compiling ACM program Practice
Running program Practice on input, producing output
Looking for input in the src directory
Done!
Look in output in the src folder for the results
- A file named output will appear in the src folder. If there are no errors and you don't see output, hit F5.
Submitting Solutions
When submitting solutions, you will find your Java files inside the
workspace directory. Submit the java file for the problem
(Practice.java).
Java Examples of how to read input
Some input will terminate after a known set of values. Some will
terminate after reading an unknown amount of input.
// Read in an unknown amount of input and prints the input followed by the sum.
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
int x;
int sum = 0;
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
x = s.nextInt();
System.out.println(x);
sum = sum + x;
}
System.out.println(sum);
}
}
// Read in input until you see a 0 and prints the input followed by the sum.
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
int x;
int sum = 0;
Scanner s = new Scanner(System.in);
x = s.nextInt();
while(x != 0) {
System.out.println(x);
sum += x;
x = s.nextInt();
}
System.out.println(sum);
}
}