Run --> Run Configurations --> Arguments (it is the 2nd tab on the right) --> Program arguments
Answer from WuHoUnited on Stack OverflowRun --> Run Configurations --> Arguments (it is the 2nd tab on the right) --> Program arguments
Add this line in your program to accept user's input from the console:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
and then add in.readLine() beside whatever variable you want to accept input from Runtime. Lets say, you want to initialize count variable to the value 1, then, it should be written as
int count = in.readLine();
The value 1 should be inputted in the console after you run the program
You can use java.util.Scanner (API):
import java.util.Scanner;
//...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
It can also tokenize input with regular expression, etc. The API has examples and there are many others in this site (e.g. How do I keep a scanner from throwing exceptions when the wrong type is entered?).
If you are using Java 6, you can use the following oneliner to read an integer from console:
int n = Integer.parseInt(System.console().readLine());
Eclipse has a console, its typically located at the bottom of your java perspective. If its not there just go to Window->Show View->Console.
Have you ever used an input stream reader or a buffered reader?
The following is a start on how
static InputStreamReader input = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(input);
Then you can use the buffered reader for taking input during execution.
If I remember correctly, you can actually type in Eclipse's Java console and that will be sent through the standard input stream, System.in (probably depending on the application's run configuration).
However the standard way of doing this is to pass a number of strings on the command line, which will appear as the elements of the String[] parameter to your application's main method.
Alternatively you can get input in via flat files, from a database, through sockets etc. though this is, as you might expect, a lot more complex and should certainly not be considered for a "Hello World" type application. Java web services, for example, do take their input via network sockets rather than via the command line as this latter would restrict them somewhat. ;-)
Using eclipse, you need to update the Run Configuration for a given Class. Provide any arguments you want to pass into the main method.
In your parseProblem() method, you are trying to read from command line arguments.
To provide command line arguments do the following steps:
Right click on the class. Select Run As -> Run Configurations...
Double click on Java Application on the left side panel.
Go to (x)= Arguments tab on the right side panel.
Under Program Arguments field, provide your input for the program.
In your case, input argument should be something similar to:
030400000
870306001
004980000
906034000
005000300
000750204
000043700
500608032
000002080
As per the code you downloaded, "0" represents blank value.
This program is based on args[] that are given when you run the program so if you want it to run, you need to go to your cmd, go to the map your class is in and type this:
example: java Sudoku 091254876985235646586546545645646545645432435435484843521231564545648545643213541564654545313213541354384584514231354854668543215153144544384384354542135453454354451351351315584648648468 (you have to give 81 arguments because your sudoku contains 9*9 digits)
To pass arguments to a Java program via Eclipse, you should use the Arguments tab in Run > Run Configurations. Example:
Arguments

Code
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
Result
33
twitter
400.23
you can pass your input parameters in following way in Eclipse: click menu Run -> Run Configurations -> Arguments in Program arguments put your arguments, which will be loaded to "String [] args" you could just separate your arguments by space:
e.g.
1 2 3
and your args = {1,2,3}
java.util.Scanner is the best choice for this task.
From the documentation:
For example, this code allows a user to read a number from System.in:
Scanner sc = new Scanner(System.in); int i = sc.nextInt();
Two lines are all that you need to read an int. Do not underestimate how powerful Scanner is, though. For example, the following code will keep prompting for a number until one is given:
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
while (!sc.hasNextInt()) {
System.out.println("A number, please?");
sc.next(); // discard next token, which isn't a valid int
}
int num = sc.nextInt();
System.out.println("Thank you! I received " + num);
That's all you have to write, and thanks to hasNextInt() you won't have to worry about any Integer.parseInt and NumberFormatException at all.
See also
- Java Tutorials/Essential Classes/Basic I/O/Scanning and Formatting
Related questions
- How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
- How to use Scanner to accept only valid int as input
Other examples
A Scanner can use as its source, among other things, a java.io.File, or a plain String.
Here's an example of using Scanner to tokenize a String and parse into numbers all at once:
Scanner sc = new Scanner("1,2,3,4").useDelimiter(",");
int sum = 0;
while (sc.hasNextInt()) {
sum += sc.nextInt();
}
System.out.println("Sum is " + sum); // prints "Sum is 10"
Here's a slightly more advanced use, using regular expressions:
Scanner sc = new Scanner("OhMyGoodnessHowAreYou?").useDelimiter("(?=[A-Z])");
while (sc.hasNext()) {
System.out.println(sc.next());
} // prints "Oh", "My", "Goodness", "How", "Are", "You?"
As you can see, Scanner is quite powerful! You should prefer it to StringTokenizer, which is now a legacy class.
See also
- Java Tutorials/Essential Classes/Regular expressions
- regular-expressions.info/Tutorial
Related questions
- Scanner vs. StringTokenizer vs. String.Split
Here's my entry, complete with fairly robust error handling and resource management:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Simple demonstration of a reader
*
* @author jasonmp85
*
*/
public class ReaderClass {
/**
* Reads two integers from standard in and prints their sum
*
* @param args
* unused
*/
public static void main(String[] args) {
// System.in is standard in. It's an InputStream, which means
// the methods on it all deal with reading bytes. We want
// to read characters, so we'll wrap it in an
// InputStreamReader, which can read characters into a buffer
InputStreamReader isReader = new InputStreamReader(System.in);
// but even that's not good enough. BufferedReader will
// buffer the input so we can read line-by-line, freeing
// us from manually getting each character and having
// to deal with things like backspace, etc.
// It wraps our InputStreamReader
BufferedReader reader = new BufferedReader(isReader);
try {
System.out.println("Please enter a number:");
int firstInt = readInt(reader);
System.out.println("Please enter a second number:");
int secondInt = readInt(reader);
// printf uses a format string to print values
System.out.printf("%d + %d = %d",
firstInt, secondInt, firstInt + secondInt);
} catch (IOException ioe) {
// IOException is thrown if a reader error occurs
System.err.println("An error occurred reading from the reader, "
+ ioe);
// exit with a non-zero status to signal failure
System.exit(-1);
} finally {
try {
// the finally block gives us a place to ensure that
// we clean up all our resources, namely our reader
reader.close();
} catch (IOException ioe) {
// but even that might throw an error
System.err.println("An error occurred closing the reader, "
+ ioe);
System.exit(-1);
}
}
}
private static int readInt(BufferedReader reader) throws IOException {
while (true) {
try {
// Integer.parseInt turns a string into an int
return Integer.parseInt(reader.readLine());
} catch (NumberFormatException nfe) {
// but it throws an exception if the String doesn't look
// like any integer it recognizes
System.out.println("That's not a number! Try again.");
}
}
}
}
One of the simplest ways is to use a Scanner object as follows:
import java.util.Scanner;
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter a number: ");
int n = reader.nextInt(); // Scans the next token of the input as an int
// Once finished
reader.close();
You can use any of the following options based on the requirements.
Scanner class
import java.util.Scanner;
//...
Scanner scan = new Scanner(System.in);
String s = scan.next();
int i = scan.nextInt();
BufferedReader and InputStreamReader classes
import java.io.BufferedReader;
import java.io.InputStreamReader;
//...
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int i = Integer.parseInt(s);
DataInputStream class
import java.io.DataInputStream;
//...
DataInputStream dis = new DataInputStream(System.in);
int i = dis.readInt();
The readLine method from the DataInputStream class has been deprecated. To get String value, you should use the previous solution with BufferedReader
Console class
import java.io.Console;
//...
Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());
Apparently, this method does not work well in some IDEs.
You have to return a value in all branches, there are 3 possibilities:
1) Return an int in the else branch:
public int rNum(String nums, String lets){
int z;
if(nums == "yes" && lets == "yes"){
z = (int)Math.random()*36;
return z;
} else if(nums == "yes"){
z = (int)Math.random()*10;
return z;
} else if(lets == "yes"){
z = (int)Math.random()*26 + 10;
return z;
} else {
System.out.println("Sorry, you need either letters or numbers in your password.");
return -1;
}
}
2) Throw an unchecked exception
public int rNum(String nums, String lets){
int z;
if(nums == "yes" && lets == "yes"){
z = (int)Math.random()*36;
return z;
} else if(nums == "yes"){
z = (int)Math.random()*10;
return z;
} else if(lets == "yes"){
z = (int)Math.random()*26 + 10;
return z;
} else {
System.out.println("Sorry, you need either letters or numbers in your password.");
throw new RuntimeException("Sorry, you need either letters or numbers in your password.");
}
}
3) Throw a checked exception
public int rNum(String nums, String lets) throws Exception {
int z;
if(nums == "yes" && lets == "yes"){
z = (int)Math.random()*36;
return z;
} else if(nums == "yes"){
z = (int)Math.random()*10;
return z;
} else if(lets == "yes"){
z = (int)Math.random()*26 + 10;
return z;
} else {
System.out.println("Sorry, you need either letters or numbers in your password.");
throw new Exception("Sorry, you need either letters or numbers in your password.");
}
}
your method must return an int value but at your last else block you are not returning anything. return a default value there, for instance return 0 or throw an exception which says invalid input.
And also;
To compare two strings you must use its equals method.
change nums == "yes" to "yes".equals(nums)
and
lets == "yes" to "yes".equals(lets)
The identifier int is a reserved keyword in Java and cannot be used as a variable name.
I would suggest not abbreviating your attributes, but instead use full words:
int strength;
int agility;
int intelligence;
Even if there were a way to create a variable named int, the resulting code would be confusing to read later.
You can't. int isn't a valid identifier in Java, because it's a keyword.
From JLS 3.8:
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (ยง3.9), boolean literal (ยง3.10.3), or the null literal (ยง3.10.7), or a compile-time error occurs.
Compare this with str, which isn't a keyword. (And neither is String, in fact.)
As Greg says, it's clearer to use full names (strength, intelligence etc) anyway.