if you are using the Java SE6 or higher then you can make use of Console clas

   Console console = System.console();
   if (console==null){
      System.out.print("console not available ");
   }else {
      String line = console.readLine("Enter name :");
      System.out.print("your name :"+line);
   }
Answer from ajduke 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 - Reading and printing an integer value in Java is a fundamental concept used to handle user input and display output. It allows programs to interact with users by accepting numeric data and showing results. Java provides built-in support through classes like Scanner and output methods such as System.out.println(), making input-output operations simple and efficient.
Discussions

java - how to take Char/arithmetic operator as input from user without using Scanner - Stack Overflow
Thanks for having a look at my question. I wanna ask that can i take character or arithmetic operator as input from the user without using scanner? This is my code to Write a program using the More on stackoverflow.com
🌐 stackoverflow.com
How get user input without using import.util.Scanner in java
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends. More on sololearn.com
🌐 sololearn.com
3
0
January 23, 2018
parsing - taking integer input in java - Stack Overflow
I am actually new to java programming and am finding it difficult to take integer input and storing it in variables...i would like it if someone could tell me how to do it or provide with an exampl... More on stackoverflow.com
🌐 stackoverflow.com
How do I get user input without using a scanner
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
12
0
August 26, 2024
🌐
Brainly
brainly.com › computers and technology › high school › how can you take input from the user in java without using the scanner class?
[FREE] How can you take input from the user in Java without using the Scanner class? - brainly.com
A Primer for Computational Biology - Shawn T. O’Neil ... In Java, you can take input from the user without using the Scanner class by using the BufferedReader class, which reads text from an input stream efficiently.
🌐
YouTube
youtube.com › watch
Java Program to Get User Input and Print an Integer - YouTube
Learn how to take an integer input from a user in Java and print it to the console screen. In this beginner-friendly Java programming tutorial, we build a cl...
Published: March 7, 2020
🌐
Stack Overflow
stackoverflow.com › questions › 48239579 › how-to-take-char-arithmetic-operator-as-input-from-user-without-using-scanner
java - how to take Char/arithmetic operator as input from user without using Scanner - Stack Overflow
However, these are some other ways to read users input without using it: ... Create a reader variable using System.in and define the variable a using a BufferedReader for obtaining the input: InputStreamReader isr = new InputStreamReader(System.in); ...
🌐
javaspring
javaspring.net › blog › take-an-integer-input-java
Taking Integer Input in Java: A Comprehensive Guide — javaspring.net
We then use the nextInt() method to read an integer from the user. Finally, we print the entered integer and close the scanner to release resources. The BufferedReader class is another way to take input from the user in Java.
🌐
BeginnersBook
beginnersbook.com › 2017 › 09 › java-program-to-read-integer-value-from-the-standard-input
Java Program to read integer value from the Standard Input
October 25, 2022 - In this program we will see how to read an integer number entered by user. Scanner class is in java.util package. It is used for capturing the input of the primitive types like int, double etc.
Find elsewhere
🌐
UW Computer Sciences
pages.cs.wisc.edu › ~hasti › cs368 › JavaTutorial › NOTES › JavaIO_Scanner.html
Java Input and Output (I/O)
September 3, 2008 - As you can see, the Scanner class contains a method named nextInt that returns the next input data available as an int value, that is, if the next input in the input stream is a valid integer format. If the next input is not a valid integer format, an InputMismatchException is thrown. You may be wondering why are there two ways to read integers from the user.
🌐
Java2Blog
java2blog.com › take-integer-input-java
How to Take Integer Input in Java – Java2Blog
September 16, 2022 - An integer is a primitive data type. int keyword is used to define integer data type variable. Integer data types have the size of 4 bytes. The range of integer data type is -2^31 to 2^31 – 1. ... We can take input as an integer from a user, with the help of the Scanner class.
🌐
Quora
quora.com › How-do-I-take-input-from-a-user-in-Java
How to take input from a user in Java - Quora
Answer (1 of 2): There are many ways to take a user's input using Java. 1. Using Scanner to read from the CMD/Terminal.(There are other choices except the Scanner). Java User Input (Scanner class) 2. Using simple UI Interface such as Swing , JavaFx Java Swing | JTextField - GeeksforGeeks JavaFX...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-take-array-input-from-user-in-java
How to Take Array Input From User in Java? - GeeksforGeeks
July 23, 2025 - Initialize the important packages in the program mentioned in the below code such as BufferedRead, IOException and InputStremReader from java.io. In the method where we use the BufferedReader and InputStreadReader throws the IOException · Then initialize the object of InputStreamReader and pass the System.in in the constructor. Then create the object of BufferedReader and pass the InputStreamReader object in the constructor. Take the size of the array from the user. The BufferedReader read the String and we use the ParseInt() method so if the user enter other than the integer number then it will throw exception so handle it using try catch.
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.");
            }
        }
    }
}
🌐
Reddit
reddit.com › r/javahelp › how do i get user input without using a scanner
r/javahelp on Reddit: How do I get user input without using a scanner
August 26, 2024 -

I’ve been trying to get the users input with the ways that my teacher has taught us but they’re not working

Heres the way he taught us

String text = input.nextLine

My program keeps telling me that there’s something wrong with input but I don’t know how to fix it without using a scanner

Top answer
1 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 5
1
It seems that you are having problems with java.util.Scanner The wiki here has a page The Scanner class and its caveats that explains common problems with the Scanner class and how to avoid them. Maybe this can solve your problems. Please do not reply because I am just a bot, trying to be helpful. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Unstop
unstop.com › home › blog › how to take input in java | scanner class & more (+examples)
How To Take Input In Java | Scanner Class & More (+Examples)
February 27, 2025 - By reading input from the user, your program can react to real-time data, making it adaptable and more user-friendly. Hence, it is essential for you to know how to take input in Java programs. There are several methods for this, each designed for different scenarios: The Scanner Class: A part of the java.util package, the Scanner class is one of the most popular and beginner-friendly ways to take input in Java.
🌐
Reddit
reddit.com › r/learnprogramming › [java] what is the best way to read a user defined integer value?
r/learnprogramming on Reddit: [Java] What is the best way to read a user defined integer value?
August 22, 2023 -

I'm brushing up on my Java programming by going through the free Java course through the University of Finland. In the course, they teach that the only way to read a user define integer is to do the following:

int value = Integer.valueOf(scanner.nextLine());

But, back when I was in college, we were taught that using the following was okay as well:

int value = scanner.nextInt();

Which one is actually correct or are they both right?

🌐
Scaler
scaler.com › home › topics › java › how to take input in java? (with examples)
How to Take Input in Java? (With examples) - Scaler Topics
February 11, 2022 - The program demonstrates how to access and display command-line arguments passed to the Java program. The args parameter in the main() method holds the command-line arguments as an array of strings. The program prints the total number of arguments and displays each argument using a for-each loop. ... The program prompts the user to enter an integer, reads the input using the Scanner class, and stores it in the number variable.
🌐
Just Academy
justacademy.co › blog-detail › how-to-take-integer-input-in-java
How to Take Integer Input in Java by Roshan Chaturvedi | JustAcademy
8) Remember to close the `Scanner` object after you have finished reading input from the user to release system resources by calling the `close()` method: ... 9) You can also provide some error handling to guide the user on how to input integers ...
🌐
CodeScracker
codescracker.com › java › program › java-program-take-input-from-user.htm
Java Program to Get Input from User
>Java Program to Get Input from User - This article is created to cover multiple programs in Java that are based on receiving inputs from user. Get integer input in Java, How to handle with invalid inputs in Java ?, Get character input in Java, Get string input in Java