Results 1 to 4 of 4
Thread: Reading text file
- 02-22-2013, 03:22 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Reading text file
I can get my program to read exactly what's inside the text file. I want it to display the output differently and not just read what is already inside the text file and display it out. I'm not sure how to do that? example is 1234, john, doe, 100. i want it to output student grade=100 | last name= john | first name= doe | student id=1234.
so basically swapping 1234 and 100 around while adding extra wording to let people know what is 100, john, doe, and 1234.
do i need some loop to get it to read the line after the 3rd comma, then make it read 2nd word after the first comma, then the line after 2nd comma comma and then line before first comma??
here's my code.
Java Code:package edu.nyt3619.file; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; public class StudentGrade { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { // Open the file that is the first // command line parameter // checks where the path is going ?? System.out.println(System.getProperty("user.dir")); FileInputStream fstream = new FileInputStream("StudentGrades.txt"); // 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); } // Close the input stream in.close(); } catch (Exception e) {// Catch exception if any System.err.println("Error: " + e.getMessage()); } } }Last edited by FYou; 02-23-2013 at 01:11 AM.
- 02-22-2013, 03:50 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Reading text file
Why do people insist on inserting a DatainputStream into these things?
Completely unecessary.
Also, can you please wrap your code in [code] tags [/code] in order to retain the formatting.?
As for your immediate problem, write a method that takes a String (representing one of those lines).
It will process the String - split() on ',' - build a new string in the format you want, and return it.
Your code above will call this inside your loop.Please do not ask for code as refusal often offends.
- 02-23-2013, 01:06 AM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Re: Reading text file
I don't quite get what you meant. Could you show me the code? I went to this tutor and the tutor just gave me the code with the datainputstream in it and i dont really under each code line except for the part where i put the name of the txt file name. I am not a good programmer. see my attached file guys.
Last edited by FYou; 02-23-2013 at 01:12 AM.
- 02-25-2013, 10:45 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Reading text file
We don't tend to write the code here.
If you haven't done methods yet, then you'll need to process each line in your loop using the same logic:
You should be able to at least get the split part working.Java Code:for each line in the file [URL="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)"]split the String [/URL]on the ',' join the bits together int he order you want.
Please do not ask for code as refusal often offends.
Similar Threads
-
reading a text file
By The original stinger in forum New To JavaReplies: 1Last Post: 05-14-2012, 02:07 PM -
Reading from text file
By Zaadyn in forum New To JavaReplies: 2Last Post: 02-02-2012, 10:00 PM -
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
Reading a text file
By diegosened in forum New To JavaReplies: 4Last Post: 01-15-2010, 11:32 PM -
Reading two text file and sum them up
By matt_well in forum New To JavaReplies: 36Last Post: 07-22-2008, 02:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks