System.in is an instance of InputStream. You can read from it directly if you want, but the Scanner class does a bunch of work for you. Otherwise you have to deal with byte values and tokenizing the input yourself. Answer from istarian on reddit.com
🌐
W3Schools
w3schools.com › java › java_user_input.asp
Java User Input (Scanner class)
import java.util.Scanner; // Import ...ntln("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input } }...
🌐
GeeksforGeeks
geeksforgeeks.org › java › scanner-class-in-java
Scanner Class in Java - GeeksforGeeks
Scanner is a predefined class in Java used to take input from the user. It is present in the java.util package and supports different types of input such as integers, strings, float values, and characters.
Published   May 27, 2026
Discussions

java - How to use Scanner to accept only valid int as input - Stack Overflow
You don't have to parseInt or worry ... don't advance past any input, you may have to call next() if you want to skip past the "garbage", as shown above. How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)... More on stackoverflow.com
🌐 stackoverflow.com
How to get the user input in Java? - Stack Overflow
Or ask user to enter two numbers and you can add, multiply, subtract, or divide those numbers and print the answers for user inputs just like a behavior of a calculator. So there you need Scanner class. You have to import java.util.Scanner; and in the code you need to use More on stackoverflow.com
🌐 stackoverflow.com
java.util.scanner - How can I read input from the console using the Scanner class in Java? - Stack Overflow
Basically, all I want is have the scanner read an input for the username, and assign the input to a String variable. ... A simple example to illustrate how java.util.Scanner works would be reading a single integer from System.in. More on stackoverflow.com
🌐 stackoverflow.com
How to use 'scanner' in JAVA?
Number which the user will enter will be stored in getal. You know how to print the value of a variable. More on reddit.com
🌐 r/learnprogramming
3
0
September 18, 2022
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-User-Input-example-String-next-int-long-char
Java Scanner User Input Example
August 8, 2025 - By default, the scanner uses the enter key to indicate the user has finished their user input. But this can be changed by through the use of the useDelimiter() method. The following Scanner example takes a String of comma-separated values (CSVs) ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Scanner.html
Scanner (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
🌐
Medium
medium.com › @AlexanderObregon › reading-a-full-line-of-input-using-scanner-in-java-9be526f7b13e
Reading a Full Line of Input Using Scanner in Java | Medium
July 4, 2025 - System.in is an InputStream, which only understands bytes. Since Java works with characters, there has to be a decoding step before your program can work with strings. The Scanner class handles this by layering a Readable interface on top of the byte stream. Internally, it goes through an InputStreamReader, which handles the charset conversion.
Find elsewhere
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-String-input-with-the-Scanner-class
Java Scanner String input example
Notice how the output includes only the first String the Java Scanner read. The rest of the text is ignored. This is because the Scanner class tokenizes the input String based on any whitespace between words.
🌐
Zero To Mastery
zerotomastery.io › blog › java-scanner
Beginner's Guide To Java Scanner (With Code Examples) | Zero To Mastery
Now that Scanner is set up, let’s explore how to read different types of input correctly without running into skipped inputs or crashes. ... The trick is knowing when to use each method, because picking the wrong one can lead to skipped inputs, unexpected results, or program crashes. Let’s go through them one by one. User input often includes single-word responses, full names, or entire sentences. Java provides two different methods for handling this:
🌐
Programiz
programiz.com › java-programming › scanner
Java Scanner (With Examples)
It works just like taking inputs from the keyboard. We have then used the nextLine() method of the Scanner class to read a line of text from the user. Now that you have some idea about Scanner, let's explore more about it. As we can see from the above example, we need to import the ...
🌐
Medium
medium.com › @AlexanderObregon › a-beginners-guide-to-handling-user-input-in-java-with-scanner-85be33c8702d
A Beginner’s Guide to Handling User Input in Java with Scanner
March 18, 2024 - The Scanner class provides a convenient and efficient way for programs to read text input, including entire lines, words, or even specific types of data like integers and floating-point numbers.
🌐
Baeldung
baeldung.com › home › java › java io › java scanner
Java Scanner | Baeldung
January 5, 2024 - In this quick tutorial, we’ll illustrate how to use the Java Scanner class – to read input and find and skip patterns with different delimiters.
🌐
W3Schools
w3schools.com › java › java_ref_scanner.asp
Java Scanner Class Reference
Java Examples Java Videos Java ... Java Syllabus Java Study Plan Java Interview Q&A ... The Scanner class can be used to obtain data from the keyboard, files and strings....
🌐
Sololearn
sololearn.com › en › Discuss › 1038025 › java-scanner-input-help
Java scanner input help | Sololearn: Learn to code for FREE!
javasyntaxhelp · 29th Jan 2018, 11:02 AM · MaxBanter · 9 Answers · Answer · + 12 · @Swan use like this int age = sc.nextInt(); I is in upper case in nextInt(); 29th Jan 2018, 11:12 AM · GAWEN STEASY · + 18 · int age = new Scanner(System.in).nextInt(); //for reducing 1 line ☺👍 ·
🌐
Sololearn
sololearn.com › en › Discuss › 1151711 › caution-while-taking-input-using-scanner-in-java
Caution while taking input using Scanner in java | Sololearn: Learn to code for FREE!
Scanner sc=new Scanner (System.in); int a =sc.nextInt(); String s=sc.nextLine(); This code will skip sc.nextLine ,and ends only taking an integer as input, as it(.nextLine()) will consume the new line character (Enter) given by user.
🌐
Reddit
reddit.com › r/learnprogramming › how to use 'scanner' in java?
r/learnprogramming on Reddit: How to use 'scanner' in JAVA?
September 18, 2022 -

I have to learn how to code in JAVA for school, I have been trying for many hours now but I fail to understand how to to use 'Scanner'

Could anybody help me out with this code to at least teach me how I can use it right the next time? Because the answers to my mistakes is nowhere to be found even after watching countless of Youtube videos.

I have to change the code so that the number will get printed.

Sample input1: 12 > The sample output for this has to be also: 12

Sample input 2: 42 > The sample output for this also has to be: 42

Sample imput 3: -12 > The sample output for this also has to be: -12

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = New Scanner(System.in);

int getal = scanner.nextInt();

System.out.println(........)

And then I honestly have no clue what to do..

Please help and thank you in advance