Run --> Run Configurations --> Arguments (it is the 2nd tab on the right) --> Program arguments

Answer from WuHoUnited on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-read-and-print-an-integer-value-in-java
How to Read and Print an Integer Value in Java - GeeksforGeeks
May 12, 2026 - It forms the base for performing calculations, conditions, and logical operations in Java programs ยท It explains different methods like Scanner and BufferedReader to take integer input from the user and display it on the screen.
๐ŸŒ
UW Computer Sciences
pages.cs.wisc.edu โ€บ ~hasti โ€บ cs368 โ€บ JavaTutorial โ€บ NOTES โ€บ JavaIO_Scanner.html
Java Input and Output (I/O)
September 3, 2008 - Use the Integer class to parse the string of characters into an integer. int number = Integer.parseInt( input ); // converts a String into an int value The Integer class contains conversion methods for changing String data into int values and vice versa. The Integer class is one of several ...
๐ŸŒ
Java Made Easy!
java-made-easy.com โ€บ java-scanner.html
Java Input Using Java Scanner
First of all you will need to use a Java Scanner that will get this input for you. It acts like a variable but it's not one of the basic types that were talked about in the previous tutorial. Add this line into the main: It is EXTREMELY important that you get the capitalization correct, or this will not work. You will also notice that Eclipse has underlined Scanner in red, as shown in the next screenshot.
Find elsewhere
๐ŸŒ
Mathbits
mathbits.com โ€บ JavaBitsNotebook โ€บ DataBasics โ€บ Input.html
Input- JavaBitsNotebook.com
Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms of Use | JavaMathBits.com Obtaining Input ยท Most of our programming applications will need to interact with the user. We will need to both display output to, and obtain input from, the user. We will be using a command line ...
Top answer
1 of 4
6

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
2 of 4
5

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.");
            }
        }
    }
}
๐ŸŒ
YouTube
youtube.com โ€บ watch
Eclipse - Taking inputs Scanner class - YouTube
This video goes through how to take inputs in Java using the Scanner class
Published: September 15, 2012
๐ŸŒ
YouTube
youtube.com โ€บ codenerve.com
Eclipse Basics - Reading Console Input - YouTube
Basic demonstration on running and entering input into a java application. For the full tutorial go to: http://www.codenerve.com/beginner-java/lesson-9-readi...
Published: October 27, 2014
Views: 2K
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ print an integer (entered by the user)
Java Program to Print an Integer (Entered by the User) | Vultr Docs
September 27, 2024 - In this article, you will learn how to create a Java program that prompts the user to enter an integer and then prints it. Explore different examples illustrating how to implement this simple but essential task using various Java methods. Import the Scanner class which is used to read data entered by the user. Create an instance of Scanner. Prompt the user to enter an integer.
๐ŸŒ
YouTube
youtube.com โ€บ my name is gyan
Console ( Keyboard ) Input in Java Eclipse - YouTube
JAVA & WEB | Session 2 | Variables (Primitive), Data Types and Operators: https://www.youtube.com/playlist?list=PL6pxHmHF3F5JPdnEqKALRMgogwYc2szp1Watch the ...
Published: April 24, 2017
Views: 2K
๐ŸŒ
Java2Blog
java2blog.com โ€บ take-integer-input-java
How to Take Integer Input in Java โ€“ Java2Blog
September 16, 2022 - We can take input as an integer from a user, with the help of the Scanner class. In java.util package Scanner class is present.
Top answer
1 of 5
2

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.");
    }
}
2 of 5
1

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)