954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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) {
        }
rjdelight
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Loop up string tokenizer or split string

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

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
 
Slyvr
Junior Poster in Training
74 posts since Dec 2010
Reputation Points: 10
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: