1.11M Members

Reading first word of text file??

 
0
 

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) {
        }
 
0
 

Loop up string tokenizer or split string

 
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);
    }
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: