#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0

    Help with displaying output from a text file


    Hello. I can get my program to read exactly what's in the text file. But when I want to choose how I want it to be displayed while reading the data that is inside the text file. i'm not sure how to do that? any ideas guy??

    Thanks!!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The Scanner class has some methods that make it easy to read the contents of a text file.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by NormR
    The Scanner class has some methods that make it easy to read the contents of a text file.
    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());
     
            }
     
        }
     }
    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??
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    If the pieces of data on a line are separated by commas, you can use the String class's split() method to separate the data at the commas into separate Strings and put them into an array.
    If you know the position of each piece of data then you can index into the array to get that piece of data and insert it into the String that you are printing out:
    System.out.println("part1 "+ theArray[1] + ", part2 " + theArray[0] + ...);

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo