Today in class, we started by learning how to get user input:
First, we need to import java.util.Scanner;
Then, we name the Scanner: Scanner in = new Scanner(System.in);
System.out.println(in.next());
This will print the next string you input. Ex: If you input "Hello World", only "Hello" will be printed.
System.out.println(in.nextLine());
This will print the next line you input. Ex: If you input "Hello World", it would print out "Hello World".
You can also do in.nextInt(), in.nextDouble(), etc.
We can also learned how to get input from a data file:
A data file is a plain text file (a common suffix is .txt or .data). These files don't have any font or size formatting information. Data files often have multiple data columns, separated by commas, white spaces, or other characters. These separators are called "delimiters".
Reading data files:
import java.util.*;
import java.io.*;
class Main{
public static void main(String[] args) throws Exception {
File theInput = new File("input.txt");
BufferedReader br = new BufferedReader(new FileReader(theInput));
String a = br.readLine(); //this will read the first line in input.txt
System.out.println(a); }
}
Homework for this week: https://docs.google.com/forms/d/e/1FAIpQLSe1hdcr7ZYGrf7N7bEJD17EXkpqo5ckWecXsY-QhmDl1CcIPQ/viewform?usp=sf_link