Basics Flashcards
(5 cards)
1
Q
Read Text File
A
can use BufferedReader, FileReader, or Scanner class
File file = new File("C:\\file_path\\test.txt"); Scanner sc = new Scanner(file);
while (sc.hasNextLine())
System.out.println(sc.nextLine());
2
Q
Read input from console
A
can use Scanner class (java.util)
Scanner sc = new Scanner(System.in); String username = scanner.nextLine();
3
Q
length vs length()
A
.length is a property of arrays
.length() is a method call for strings
4
Q
Write to File
A
Can use PrintWriter class (java.io)
File file = new File("C:/path/file.txt"); PrintWriter pw = new PrintWriter(file); pw.println("hello")
5
Q
Binary Search
A
searches a sorted array by repeatedly dividing the search interval in half