• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

coding problem!!

 
Greenhorn
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:\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();

}
}
 
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
st.substring(pos);
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.
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic