Results 1 to 4 of 4
  1. #1
    FYou is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default 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());
    
    		}
    
    	}
    
    }
    Attached Files Attached Files
    Last edited by FYou; 02-23-2013 at 01:11 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    FYou is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,049
    Rep Power
    20

    Default 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:
    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.
    You should be able to at least get the split part working.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. reading a text file
    By The original stinger in forum New To Java
    Replies: 1
    Last Post: 05-14-2012, 02:07 PM
  2. Reading from text file
    By Zaadyn in forum New To Java
    Replies: 2
    Last Post: 02-02-2012, 10:00 PM
  3. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  4. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 PM
  5. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •