Reputation: 411
Im new to Java and am trying to make a QuickUnion algorithm run. I have these text files on my desktop and want to program to read the integers in them. This is the end of the QuickUnion class.
public static void main(String[] args) {
int N = StdIn.readInt(); // Read number of sites
QuickUnionUF quickunion = new QuickUnionUF(N);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt(); // Read pair to connect
if (quickunion.connected(p, q)) continue; // Ignore if connected
quickunion.union(p, q); // Combine components
StdOut.println(p + " " + q); // and print connection
}
StdOut.println(quickunion.count() + " components");
}
My question is: how does StdIn work? How do I read the text file? the first test file contains two columns of numbers.
Upvotes: 0
Views: 53459
Reputation: 719279
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.
Upvotes: 2
Reputation: 111
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.
Upvotes: 1