File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes coding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

coding problem

Stephen Barrow
Greenhorn

Joined: Mar 03, 2004
Posts: 13
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();
}
}

--------------------------------------------------------------------------------
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
This is a duplicate post.
Please continue in the Beginners forum...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: coding problem