W3Schools
w3schools.com › java › java_user_input.asp
Java User Input (Scanner class)
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings: import java.util.Scanner; ...
W3Schools
w3schools.com › java › java_ref_scanner.asp
Java Scanner Class Reference
clear() clone() compute() computeIfAbsent() computeIfPresent() containsKey() containsValue() entrySet() forEach() get() getOrDefault() isEmpty() keySet() merge() put() putAll() putIfAbsent() remove() replace() replaceAll() size() values() Java ...
Videos
Intro to Java
redi-school.github.io › intro-java › lesson4 › scanner.html
Scanner - Intro to Java
The following exercises shall introduce you to Scanner which is used to get input from the user. Please do the following exercises. Feel free to discuss your solutions with your neighbours. Write the things you found out down to share them later with the class. import java.util.Scanner; public class ScannerExperiments { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("What is your name?"); String name = input.nextLine(); System.out.println("It's me, " + name + "!"); } }
GeeksforGeeks
geeksforgeeks.org › java › scanner-class-in-java
Scanner Class in Java - GeeksforGeeks
Exercises · Examples · Quizzes · Projects · Cheatsheet · DSA in Java · Java Collection · Sign In ▲ · Open In App · Last Updated : 23 Jul, 2025 · Comments · Improve · Suggest changes · 234 Likes · Like · Report · In Java, the Scanner class is present in the java.util package is used to obtain input for primitive types like int, double, etc., and strings.
Published July 23, 2025
Oxford University
mathcenter.oxford.emory.edu › site › cs170 › probSetScannerRandomAndFileClasses
Exercises - Scanner, Random, and File Classes
import java.util.Random; import java.util.Scanner; public class RandomPoint { public static void main(String[] args) { // used to generate random numbers.. Random random = new Random(); // used to collect input from user.. Scanner scanner = new Scanner(System.in); System.out.println("minimum x ?
Programiz
programiz.com › java-programming › scanner
Java Scanner (With Examples)
The Scanner class of the java.util package is used to read input data from different sources like input streams, users, files, etc. In this tutorial, we will learn about the Java Scanner and its methods with the help of examples.
Zero To Mastery
zerotomastery.io › blog › java-scanner
Beginner's Guide To Java Scanner (With Code Examples) | Zero To Mastery
The more you practice, the more intuitive Scanner will become. So put your knowledge to the test and start coding 😁. Remember, if you want to fast-track your Java knowledge and get as much hands-on practice as possible, then check out my Java programming bootcamp: Updated for 2025, you'll learn Java programming fundamentals all the way from complete beginner to advanced skills. Better still, you’ll be able to reinforce your learning with over 80 exercises ...
Oracle
docs.oracle.com › javase › tutorial › essential › io › scanning.html
Scanning (The Java™ Tutorials > Essential Java Classes > Basic I/O)
By default, a scanner uses white space to separate tokens. (White space characters include blanks, tabs, and line terminators. For the full list, refer to the documentation for Character.isWhitespace.) To see how scanning works, let's look at ScanXan, a program that reads the individual words in xanadu.txt and prints them out, one per line. import java.io.*; import java.util.Scanner; public class ScanXan { public static void main(String[] args) throws IOException { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("xanadu.txt"))); while (s.hasNext()) { System.out.println(s.next()); } } finally { if (s != null) { s.close(); } } } }
Learnjavathehardway
learnjavathehardway.org › book › ex07.html
Exercise 7: Getting Input from a Human
On line 8 we ask the Scanner object named keyboard to do something for us. We say “Keyboard, run your next() function.” The Scanner object will pause the program and wait for the human to type something.
W3Schools
w3schools.com › java › ref_scanner_next.asp
Java Scanner next() Method
Java Examples Java Compiler Java ... Q&A Java Certificate ... // Create a scanner object Scanner myObj = new Scanner("A string to scan"); // Output the next token System.out.println(myObj.next());...
Software Testing Help
softwaretestinghelp.com › home › java › java scanner class tutorial with examples
Java Scanner Class Tutorial With Examples
April 1, 2025 - As the name is a string, the Scanner object uses the next () method. For class input, it uses nextInt () while for percentage it uses nextFloat (). In this manner, you can easily segregate the input while reading. The output of the program shows the input being entered and the information displayed. import java.util.*; public class Main{ public static void main(String []args){ String name; int myclass; float percentage; //creating object of Scanner class Scanner input = new Scanner(System.in); System.out.print("Enter your name: "); name = input.next(); System.out.print("Enter your class: "); myclass = input.nextInt(); System.out.print("Enter your percentage: "); percentage = input.nextFloat(); input.close(); System.out.println("Name: " + name + ", Class: "+ myclass + ", Percentage: "+ percentage); } }
GeeksforGeeks
geeksforgeeks.org › java › java-user-input-scanner-class
Java User Input - Scanner Class - GeeksforGeeks
Exercises · Examples · Quizzes · Projects · Cheatsheet · DSA in Java · Java Collection · Sign In ▲ · Open In App · Last Updated : 23 Jul, 2025 · Comments · Improve · Suggest changes · 173 Likes · Like · Report · Try it on GfG Practice · The most common way to take user input in Java is using the Scanner class.
Published July 23, 2025
HWS Math
math.hws.edu › javanotes › c2 › ex3-ans.html
Javanotes 9, Solution to Exercise 3, Chapter 2
import java.util.Scanner; public class GreetingWithScanner { public static void main(String[] args) { Scanner stdin = new Scanner( System.in ); String usersName; // The user's name, as entered by the user. String upperCaseName; // The user's name, converted to upper case letters.
W3Schools
w3schools.com › java › ref_scanner_findwithinhorizon.asp
Java Scanner findWithinHorizon() Method
Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Certificate ... // Create a scanner object Scanner myObj = new Scanner("Please send an email to [email protected] for more details."); // Get the email address with a pattern String email = myObj.findWithinHorizon("[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]{2,}", 0); // Show the email if found if (email != null) { System.out.println(email); } else { System.out.println("No email found"); }
W3Schools
w3schools.com › java › ref_scanner_nextint.asp
Java Scanner nextInt() Method
// Create a scanner object Scanner myObj = new Scanner("An int is a number between -2,147,483,648 and 2,147,483,647"); // Print the value of every int in the scanner while(myObj.hasNext()) { if(myObj.hasNextInt()) { System.out.println(myObj.nextInt()); } else { myObj.next(); } }...
W3Schools
w3schools.com › java › ref_scanner_close.asp
Java Scanner close() Method
Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... import java.io.File; // Import the File class import java.io.FileNotFoundException; // Import this class to handle errors import java.util.Scanner; // Import the Scanner class to read text files public class ReadFile { public static void main(String[] args) { try { File myObj = new File("filename.txt"); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); System.out.println(data); } myReader.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } } Run Example »