📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
- Scanner class overview
- How does a Scanner work?
- How to use the Java Scanner class to read input
- How to read the file line by line with an example
- Convert InputStream to String
Video Tutorial
1. Scanner class overview
The below class diagram shows all the APIs/methods of Scanner class:
2. How does a Scanner work?
System.out.println("Enter string input: ");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
How to use the Java Scanner class to read input
package net.javaguides.examples;
import java.util.Scanner;
/**
* Scanner class examples
* @author Ramesh Fadatare
*
*/
public class ScannerExamples {
public static void main(String[] args) {
// Create Scanner object
Scanner scanner = new Scanner(System.in);
// read user input as string
System.out.println("Enter string input: ");
scanner.nextLine();
// read user input as integer
System.out.println("Enter integer input");
scanner.nextInt();
// read user input as long
System.out.println("Enter long input");
scanner.nextLong();
// read user input as float
System.out.println("Enter float input");
scanner.nextFloat();
// read user input as byte
System.out.println("Enter byte input");
scanner.nextByte();
// read user input as short
System.out.println("Enter short input");
scanner.nextShort();
// read user input as boolean
System.out.println("Enter boolean input");
scanner.nextBoolean();
// read user input as BigDecimal
System.out.println("Enter BigDecimal input");
scanner.nextBigDecimal();
// read user input as BigInteger
System.out.println("Enter BigInteger input");
scanner.nextBigInteger();
scanner.close();
}
}
Enter string input:
Ramesh
Enter integer input
100
Enter long input
120
Enter float input
12
Enter byte input
12
Enter short input
1
Enter boolean input
true
Enter BigDecimal input
120.01
Enter BigInteger input
12346
4. How to read the file line by line with an example
package net.javaguides.examples;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
* Scanner class file read example
* @author Ramesh Fadatare
*
*/
public class ScannerFileReadExample {
public static void main(String[] args) throws FileNotFoundException {
String fileName = "sample.txt";
try (Scanner scanner = new Scanner(new File(fileName))) {
while (scanner.hasNext()) {
String line = scanner.nextLine();
System.out.println(line);
}
}
}
}
5. Convert InputStream to String
package net.javaguides.examples;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerConvertInputStreamToString {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream inputStream
= new FileInputStream("sample.txt");
Scanner scanner = new Scanner(inputStream);
scanner.useDelimiter("A");
String result = scanner.next();
System.out.println(result);
scanner.close();
}
}
Hello
Java Guides
Comments
Post a Comment
Leave Comment