Input/Output Example

PHOTO EMBED

Sun Jan 16 2022 06:11:04 GMT+0000 (Coordinated Universal Time)

Saved by @mikemcnik #java

/* 
TXT file contains these characters:
Pebbles Flintstone\n
1 2.2\n
This is a line of text.\n
*/

import java.io.File;
import hava.util.Scanner;

Scanner scanner = new Scanner(new File("input.txt"));
String s1 = scanner.next();  //s1 is assigned "Pebbles"
String s2 = scanner.next(); // s2 is assigned "Flintstone"
int x = scanner.nextInt(); // x is assigned 1
double y = scanner.nextDouble(); // y is assigned 2.2
scanner nextLine(); //Advances scanner to beginning of next line
String s3 = scanner.nextLine(); // s3 is assigned "This is a line of text"
scanner.close();
content_copyCOPY

Example of reading a TXT file and assigning variables.