Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

coding problem!!

Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:\ incompatible types
found : int
required: java.lang.String
s = st.lastIndexOf(" ");
A:\ cannot resolve symbol
symbol : method substring (java.lang.String)
location: class java.lang.String
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.*;
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;
for (int i = 0; i < fileSize; i++) {
//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(" ");

Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the method lastIndexOf() returns a number of type int. It is an index (Position), just like the name states.
So store that index to an int, say pos.
Then this
gives you the string at that index - whith any luck, this is that gpa-thing your looking for. If you know the exact length of gpa's, say 3, use
st.substring(pos, pos+3);
Good luck!
Tobias Hess
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't crosspost, please.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic