The Std library does not provide the functionality that is needed to read from an arbitrary file. Its purpose is to provide an easy way for beginners (students) to write simple programs that read and write to standard input / standard output. It has limited functionality1.
By contrast, production code uses the standard System.in and System.out, either directly or (in the case of System.in) via the Scanner class. So to read text from a file (or standard input) in production code, you would typically do something like this:
Scanner in = new Scanner(new File("/some/path/to/file.txt"));
or
Scanner in = new Scanner(System.in);
and then use the various Scanner methods to either read lines or individual items.
(You could also use the Reader or BufferedReader APIs, and parse the input lines yourself in various ways. Or use an existing parser library to read CSV files, JSON files, XML files and so on.)
Your example would look something like this:
import java.util.Scanner;
...
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // replace as required!
int n = in.nextInt(); // Read number of sites
QuickUnionUF quickUnion = new QuickUnionUF(n);
while (in.hasNextInt()) {
int p = in.nextInt();
int q = in.nextInt(); // Read pair to connect
if (quickUnion.connected(p, q)) continue; // Ignore if connected
quickUnion.union(p, q); // Combine components
System.out.println(p + " " + q); // and print connection
}
System.out.println(quickUnion.count() + " components");
}
Note that new Scanner(new File(someString)) is declared as throwing FileNotFoundException which your code must deal with.
1 - My advice would be to stop using StdIn, StdOut and the rest as soon as you can. Learn to use the standard APIs, and switch.
The Std library does not provide the functionality that is needed to read from an arbitrary file. Its purpose is to provide an easy way for beginners (students) to write simple programs that read and write to standard input / standard output. It has limited functionality1.
By contrast, production code uses the standard System.in and System.out, either directly or (in the case of System.in) via the Scanner class. So to read text from a file (or standard input) in production code, you would typically do something like this:
Scanner in = new Scanner(new File("/some/path/to/file.txt"));
or
Scanner in = new Scanner(System.in);
and then use the various Scanner methods to either read lines or individual items.
(You could also use the Reader or BufferedReader APIs, and parse the input lines yourself in various ways. Or use an existing parser library to read CSV files, JSON files, XML files and so on.)
Your example would look something like this:
import java.util.Scanner;
...
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // replace as required!
int n = in.nextInt(); // Read number of sites
QuickUnionUF quickUnion = new QuickUnionUF(n);
while (in.hasNextInt()) {
int p = in.nextInt();
int q = in.nextInt(); // Read pair to connect
if (quickUnion.connected(p, q)) continue; // Ignore if connected
quickUnion.union(p, q); // Combine components
System.out.println(p + " " + q); // and print connection
}
System.out.println(quickUnion.count() + " components");
}
Note that new Scanner(new File(someString)) is declared as throwing FileNotFoundException which your code must deal with.
1 - My advice would be to stop using StdIn, StdOut and the rest as soon as you can. Learn to use the standard APIs, and switch.
Standard library (Std) is often provided for students who are taking their first course in programming in Java. Std library is not part of "installed java libraries" therefore in order to use it you have to download the Std library and declare it in your path. It works identical to Java Scanner class. Consider
public static void main(String[] args) {
StdOut.print("Type an int: ");
int a = StdIn.readInt();
StdOut.println("Your int was: " + a);
StdOut.println();
}
}
Which takes in only ONE integer, a , and prints it out. How ever as I see you're using isEmpty()which returns true if standard input is empty (except possibly for whitespace). Therefore you can use the isEmpty() to take in as many input as you want. Here is an example code of this usage
public static void main(String[] args) {
double sum = 0.0; // cumulative total
int n = 0; // number of values
while (!StdIn.isEmpty()) {
double x = StdIn.readDouble();
sum = sum + x;
n++;
}
StdOut.println(sum / n);
}
Which calculates instantly the average of your inputs and prints it out as you type in new input.
Since Java 8, you can read all lines into a Stream<String>:
private static String getInput() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
return reader.lines().collect(Collectors.joining("\n"));
}
If you want to keep the newlines, you can use an overload for Collectors.joining(), like Collectors.joining("\n")
The problem with interactive input is: You may not know if there is an other line coming. So you have to signal end-of-file using your operating system input.
On windows this is the sequence Ctrl+Z, while on Linux it's Ctrl+D
You can not read null from System.in but you can terminate the input on an empty string and then return null if the length of the input is 0.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println(getInput());
}
private static String getInput() {
StringBuilder sb = new StringBuilder();
final Scanner scanner = new Scanner(System.in);
for (String line = scanner.nextLine(); !line.isEmpty(); line = scanner.nextLine()) {
sb.append(line).append(System.lineSeparator());
}
return sb.length() == 0 ? null : sb.toString();
}
}
A sample run:
Hello
World
How are you?
Hello
World
How are you?
Another sample run:
null