Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 22nd, 2013, 12:22 AM
FYou FYou is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 13 FYou User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 21 m 37 sec
Reputation 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!!

Reply With Quote
  #2  
Old February 22nd, 2013, 05:55 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
The Scanner class has some methods that make it easy to read the contents of a text file.

Reply With Quote
  #3  
Old February 22nd, 2013, 06:26 PM
FYou FYou is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 13 FYou User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 21 m 37 sec
Reputation Power: 0
Quote:
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??

Reply With Quote
  #4  
Old February 22nd, 2013, 06:36 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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] + ...);

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help with displaying output from a text file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap