OK I'm trying to make a program using i/o that will read 5 records from a text file C:\\hw4\\rawData.txt. They are in the format of name, age, sex, and gpa ex=John Doe 25 M 3.2 not separators just a
string.
I have to write a method that will display all five on the screen. That I have done the first part of the code will display the five record lines of the text file. Then I have to use indexOf and get the gpa's out of each line and then insert them into another file I create C:\\test\\write.txt. That is what I am having extreme trouble on. I have asked for help before in a previous post and have tried to use some of the things that I learned there but I am still stuck My code is below. Give me some pointers on how to do this. I'm not asking anyone to do my work. I have some code I just need to know how to make it work and more importantly WHY it is not working. Thanks you all for you knowledge.
Ok I have my new code below. I put everything under the main method check the code to see if it is working. I will seperate the code into the appropriate methods later. The problem now is that I get the following errors:
A:\HW_4_Code2.java:36: incompatible types
found : int
required: java.lang.String
s = st.lastIndexOf(" ");
^
A:\HW_4_Code2.java:37: cannot resolve symbol
symbol : method substring (java.lang.String)
location: class java.lang.String
st.substring(s);
^
2 errors
Process completed.
When I change s to an int type the code works but when the new file is created it inputs numbers that are not the gpa. Example would be that the gpa is 3.0 but the file lists 17? Why is it doing this?
import javax.swing.*;
import java.io.*;
public class HW_4_Code2 {
public static void main(String[] args) throws IOException {
//setup file and stream
File inFile = new File("C:\\hw4\\rawData.txt");
FileInputStream inStream = new FileInputStream(inFile);
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new BufferedReader(fileReader);
//String str, s;
//set up an array to read data in
int fileSize = (int)inFile.length();
byte[] byteArray = new byte[fileSize];
//read data in and display them
inStream.read(byteArray);
for (int i = 0; i < fileSize; i++) {
System.out.print((char)byteArray[i]);
inStream.close();
}
//Suppose to take all five gpa's from file and then input them
//into another file.
//public void processFile(String inFile) throws IOException{
File outFile = new File("C:\\test\\write");
BufferedReader in = new BufferedReader(new FileReader(inFile));
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
String st, s;
while ((st = in.readLine()) != null){
st = bufReader.readLine();
s = st.lastIndexOf(" ");
st.substring(s);
outStream.println(s);
}
in.close();
outStream.close();
}
}