In Java, you can use the nextLine method of the Scanner class to read a line of input from the user. If you want to skip a line, you can simply call the nextLine
method without assigning the result to a variable. In this short article, we will discuss how to skip a line in Java scanner by solving various examples.
Introduction to Scanner Object in Java
In Java, the Scanner class is used to read input from various sources, such as the keyboard, a file, or a string. The Scanner class provides a number of methods for reading different types of input, such as nextInt for reading an integer, nextDouble for reading a double, and nextLine for reading a line of text.
Before going to discuss how we can skip a line in a Java scanner, let us first understand how to create a Scanner object in Java. Here we will go through various possible ways to create a Scanner object in Java programing language.
Reading from the Keyboard:
To create a Scanner object that reads from the keyboard, you can pass System.in to the constructor as shown below:
Scanner sc = new Scanner(System.in);
The System.in object is a standard input stream that represents the keyboard. By passing it to the Scanner constructor, you create a Scanner object that reads input from the keyboard.
Reading from a File:
To create a Scanner object that reads from a file, you can pass a File object to the constructor as shown below:
File file = new File("file.txt");
Scanner sc = new Scanner(file);
The File object represents the file you want to read from, and the Scanner object reads input from the file.
Reading from a String
Similar to previous methods, we can create a Scanner object from a string by simply passing the string to the constructor.
String s = "Hello, world!";
Scanner sc = new Scanner(s);
Method-1: Using scanner.nextLine()
The nextLine method of the Java Scanner class is used to read a line of text from the input source. The method returns the line of text as a String, excluding the line separator at the end.
Here's an example of using the nextLine
method to read a line of text from the keyboard:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a line of text: ");
String line = sc.nextLine();
System.out.println("You entered: " + line);
}
}
In this example, the program prompts the user to enter a line of text, and then it reads the line using the nextLine method of the Scanner class and assigns the result to the line variable. The contents of the line variable are then printed to the console.
To skip a line in the Scanner class, you can use the nextLine
method in a loop, but simply not use the returned value. For example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a line to be skipped: ");
sc.nextLine();
System.out.print("Enter a line of text: ");
String line = sc.nextLine();
System.out.println("You entered: " + line);
}
}
In this example, the program prompts the user to enter a line of text to be skipped, reads the line using the nextLine method, but does not use the returned value. The program then prompts the user to enter another line of text, reads the line using the nextLine method, and assigns the result to the line variable. The contents of the line variable are then printed to the console.
Note that the nextLine method blocks until the end of the line is reached, so it can be used to read a line of text from any input source, including the keyboard, a file, or a string.
Method-2: Using scanner.skip()
The scanner.skip()
method can be used to skip a specific pattern in a file as well. When using Scanner
with a file, you can pass the File
object as a parameter to the Scanner
constructor to read input from the file.
Here's an example that reads a text file and skips lines that start with a "#
" character:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SkipLineInFileExample {
public static void main(String[] args) {
File file = new File("input.txt");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#")) {
// Skip the line that starts with "#"
scanner.skip(".*\n");
} else {
// Process the line
System.out.println(line);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
In this example, we first create a File
object representing the input file "input.txt". We then create a Scanner
object and pass the File
object as a parameter to the constructor.
Inside the while
loop, we read each line from the file using scanner.nextLine()
, and check if it starts with a "#" character using the startsWith()
method. If it does, we skip the entire line by calling scanner.skip(".*\n")
.
The regular expression ".*\n" passed as argument to the scanner.skip()
method matches any characters until the end of the line, effectively skipping the entire line. If the line doesn't start with a "#
" character, we process it by printing it to the console.
Method-3: Using continue
statement
Here's an example of how we can use continue
to skip lines that start with the "#" character:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SkipLineInFileExample {
public static void main(String[] args) {
File file = new File("input.txt");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#")) {
// Skip the line that starts with "#"
continue;
}
// Process the line
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
In this example, we read input from a file named "input.txt" using a Scanner
object. Inside the while
loop, we use an if
statement to check if the current line starts with the "#" character. If it does, we use the continue
statement to skip to the next iteration of the loop, which reads the next line from the file.
If the line does not start with "#", we process the line by printing it to the console using System.out.println()
.
Summary
In this article, we learned how to skip a line in Java using the Scanner class. The Scanner class provides a number of methods for reading different types of inputs. We covered how we can skip a line using nextLine()
method in Java.
Further Reading
Java ScannerÂ
How to skip reading a line with scanner