- #1
Sam032789
- 11
- 0
Hello, I am just starting to learn Java. I have a small file that needs reading in, line by line, with different arguments. Such a line looks like this:
I need to read these in as separate variables. I already have part of my code:
The "if (strLine == expectedArg)" part returns an error. I know it should be strLine.something but I'm not sure what. Any suggestions? Also, will this work in assigning the file arguments to variables?
Code:
123 Midterm .25 100
456 TakehomeExam .25 200
0 Quiz03 .10 25
1 Final .40 100
I need to read these in as separate variables. I already have part of my code:
Code:
import java.io.*;
public class Program {
public static void main(String args[]) {
double id = 0.0;
String label = "";
double weight = 0.0;
double graduate = 0.0;
double undergrad = 0.0;
int expectedArg = 5;
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("exams.dat");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//System.out.println (strLine);
//Try to put the args into the variables.
if (strLine == expectedArg) {
try {
id = Double.parseDouble(args[0]);
label = args[1];
weight = Double.parseDouble(args[2]);
graduate = Double.parseDouble(args[3]);
undergrad = Double.parseDouble(args[4]);
} catch (Exception e) {
System.out.println("Error: ");
e.printStackTrace();
System.exit(1);
}
}
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
The "if (strLine == expectedArg)" part returns an error. I know it should be strLine.something but I'm not sure what. Any suggestions? Also, will this work in assigning the file arguments to variables?