import java.util.Scanner; public class Example { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int value = scanner.nextInt(); System.out.println("The value entered is: " + value); } }
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Example { public static void main(String[] args) { File file = new File("input.txt"); try { Scanner scanner = new Scanner(file); while(scanner.hasNext()) { int value = scanner.nextInt(); System.out.println("Value read from file: " + value); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }In this example, a file named "input.txt" containing integer values is read using Scanner.nextInt(). The while loop reads each integer value in the file and prints it to the console. The code uses the java.io.File package to access the input file. The java.util.Scanner package library provides various methods to read data from different input sources like standard input, files, etc. and convert them into different data types like integers, doubles, strings, etc.