import java.io.File; import java.io.IOException; import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { try { File inputFile = new File("input.txt"); Scanner scanner = new Scanner(inputFile); scanner.useDelimiter("\\|"); while(scanner.hasNext()){ System.out.println(scanner.next()); } scanner.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); scanner.useDelimiter(","); while(scanner.hasNext()){ System.out.println(scanner.next()); } scanner.close(); } }In this example, we use the Scanner class to read user input tokens separated by "," delimiter. The hasNext and next methods are used to loop over the input tokens and print them to the console. Package Library: java.util