We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading first word of text file??

I have a simple text file that I am reading the contents of in a GUI. It's a simple program that enables a user to browse the file and add to it if needed. I have this part running fine, but I'm having some difficulty reading just the first word of the first line and the first word of the last line.

Basically if I have a text file like this:

California, West, 1981, 115, 25.99
New York, East, 1991, 120, 19.95

I want to be able to call the first word of the first line (California) and the first word of the last line (New York). Any suggestions for making this happen? Thanks a bunch!

Here's my code:

String line = "";
        FileReader fWriter = null;
        BufferedReader writer = null;
        try {
            fWriter = new FileReader("info.txt");
            writer = new BufferedReader(fWriter);
        List<String> list = new ArrayList<String>();
          while((line = FileReader.readLine())!= null){
                 list.add(line);
         }
 list.get(0); // first line
 list.get(list.size()-1); // last line
          
        } catch (Exception e) {
        }
2
Contributors
3
Replies
7 Hours
Discussion Span
2 Years Ago
Last Updated
4
Views
rjdelight
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Loop up string tokenizer or split string

Slyvr
Junior Poster in Training
74 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

I've been messing around with this for a while and I've changed a few things, and managed to get the first and last lines to display. I'm still stuck on getting the first word from those lines.

Updated code:

throws Exception {
        FileReader one = new FileReader ("info.txt");
        BufferedReader in = new BufferedReader(new FileReader("info.txt"));
        first = in.readLine();
        while ((line = in.readLine()) != null) {
            if (line != null) {
                last = line;
            }
        }
        System.out.println(first);
        System.out.println(last);
    }
rjdelight
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Slyvr
Junior Poster in Training
74 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0624 seconds using 2.66MB