1 2 Previous Next 22 Replies Latest reply: Dec 29, 2007 7:44 PM by 807601 RSS

    read the first word of each line in a text file

    807601
      i need to read the first word of each line of a text file.
      i know of line.split() but not actually sure how i would go about using it

      Any help most appreciated

      Many Thanks

      Ben
        • 1. Re: read the first word of each line in a text file
          807601
          The easiest solution, if you're using a real operating system, would be to use awk. Awk is a text-processing program, included on real operating systems, that is very handy for this sort of thing.

          If this is a homework assignment and you can't use awk...then probably you should read in each line using a java.util.Scanner, then split the line using String.split or you could possibly just use String.indexOf if you can make some assumptions about the input file (e.g., that words are separated by spaces and not punctuation or tabs).
          • 2. Re: read the first word of each line in a text file
            807601
            Hi thanks for the reply!

            this is what i tried... and it still doesn't get me the first word of each line!
            import java.awt.*;
            import javax.swing.*;
            import java.awt.event.*;
            import java.awt.Color;
            import java.awt.event.ActionEvent;
            import java.io.*;
            import java.io.IOException;
            import java.io.InputStream;
            import java.io.InputStreamReader;
            import javax.swing.text.*;
            import javax.swing.event.*;
            import java.util.Calendar;
            import java.util.Scanner;
            import java.util.Vector;
            import java.text.SimpleDateFormat;
            import java.net.InetAddress;
            import java.net.UnknownHostException;
            import java.io.BufferedReader;
            
            
            
            
            
            
            public class testing {
                 Vector progs=new Vector();
                 Vector rand=new Vector();
                 String[] tmp = new String [2];
                 String str;
                 String str2="ewerwer";
                 String str3;
                 public void programTest()
                 {
                           try 
                                   {
                                     BufferedReader in = new BufferedReader(new FileReader("progList.log"));
                                     
                                                    
                                     while ((str = in.readLine()) != null) 
                                     {
                                          progs.add(str);
                                     }
                                }
                                catch(IOException e)
                                {
                                     System.out.println("cannot read file");
                                }
                      
                 //////THE ISSUES IS HERE....I WANT TO GET THE FIRST WORD FROM EACH LINE OF THE FILE!!!     
                      try 
                                   {
                                     BufferedReader in2 = new BufferedReader(new FileReader("eventLog.log"));
                                     
                                                    
                                     while ((str = in2.readLine()) != null) 
                                     {
                                          tmp = str.split(" ");
                                          System.out.println(tmp[0]);
                                     }
                                }
                                catch(IOException e)
                                {
                                     System.out.println("cannot read file");
                                }
                      
                      
                                          
                 
                 }
            public static void main(String[] args) 
              
                 {
                         testing B = new testing();
                         B.programTest();
                       //  B.fileToVector();
                         //B.LoginWindow();
                       //B.anomDetect();
               
                 }
            
            
            }//end class
            • 3. Re: read the first word of each line in a text file
              795426
              bensegar wrote:
              Hi thanks for the reply!

              this is what i tried... and it still doesn't get me the first word of each line!
              Okay, what does it give you? Sample input and output would be a Good Idea to include.
              Additionally, initializing tmp is pointless, as you're just going to throw away whatever is there when you assign split()'s result to it.

              Incidentally, what's with those imports at the top? You're importing a lot of stuff you never use, some of them multiple times.
              Also, get rid of the import java.foo.* lines. they lead to name collisions. Just import the specific classes you use in that file.
              • 4. Re: read the first word of each line in a text file
                807601
                hi thanks for speedy reply!

                the reason for those imports is because i just copied it from one of my other programs.

                basically i compare the first term of this text file with stuff from a vector. all i need it to do is get the first term and i think i can take it from there...

                really want to get it sorted today because its been bugging me since midday!!!
                • 5. Re: read the first word of each line in a text file
                  807601
                  my file contains:

                  hello my name is
                  i live at
                  this house belongs to


                  so my output would be:

                  hello
                  i
                  this


                  any ideas?
                  • 6. Re: read the first word of each line in a text file
                    795426
                    Okay, but what does the code you posted output?
                    • 7. Re: read the first word of each line in a text file
                      807601
                      ok i am reposting code. this time without the rubbish.

                      i want this code to look at my txt file and find the first word.
                      it then puts this word into my array.

                      how this code just takes the splits the whole text file via ' ' and sticks it in point 0 of my array.
                      import java.io.*;
                      import java.io.IOException;
                      import java.io.InputStream;
                      import java.io.InputStreamReader;
                      import java.io.BufferedReader;
                      
                      public class testing {
                           
                           String[] tmp = new String [2];
                           String str;
                           public void programTest()
                           {
                                     
                                
                                
                                try 
                                             {
                                               BufferedReader in2 = new BufferedReader(new FileReader("eventLog.log"));
                                               
                                                              
                                               while ((str = in2.readLine()) != null) 
                                               {
                                                    tmp = str.split(" ");
                                                    System.out.println(tmp[0]);
                                               }
                                          }
                                          catch(IOException e)
                                          {
                                               System.out.println("cannot read file");
                                          }
                                
                                
                                                    
                           
                           }
                      public static void main(String[] args) 
                        
                           {
                                   testing B = new testing();
                                   B.programTest();
                                
                         
                           }
                      
                      
                      }//end class
                      • 8. Re: read the first word of each line in a text file
                        807601
                        It works.
                        But what is your question?
                        • 9. Re: read the first word of each line in a text file
                          807601
                          If you mean how the split works:

                          read:

                          http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
                          • 10. Re: read the first word of each line in a text file
                            807601
                            i only want the first word to go into array.
                            • 11. Re: read the first word of each line in a text file
                              807601
                              bensegar wrote:
                              i only want the first word to go into array.
                              You mean the array produced by split?

                              Just let split do what it does, and then take the word out of the first array position, and put it somewhere else, say another array or a java.util.List, or just print it out if that's all you're going to do with it. Then move on.
                              • 12. Re: read the first word of each line in a text file
                                807601
                                hey,
                                well this code....did look at my file (which consisted of 18 lines)
                                and my output was 18 "null" lines!

                                Please help me anyone!!!!
                                import java.io.*;
                                import java.io.IOException;
                                import java.io.InputStream;
                                import java.io.InputStreamReader;
                                import java.io.BufferedReader;
                                import java.util.Vector;
                                public class testing {
                                     String str;
                                     String testy;
                                     public void programTest()
                                     {
                                          try 
                                                       {
                                                         BufferedReader in2 = new BufferedReader(new FileReader("eventLog.log"));
                                                         
                                                                        
                                                         while ((str = in2.readLine()) != null) 
                                                         {     
                                                                   String thing[]=str.split(" ");
                                                                   thing[0]=testy;
                                                                   System.out.println(testy);
                                                                        
                                                         }
                                                    
                                                    }
                                          catch(IOException e)
                                                    {
                                                         System.out.println("cannot read file");
                                                    }
                                          
                                          
                                                              
                                     
                                     }
                                public static void main(String[] args) 
                                  
                                     {
                                             testing B = new testing();
                                             B.programTest();
                                          
                                   
                                     }
                                
                                
                                }//end class
                                • 13. Re: read the first word of each line in a text file
                                  807601
                                  I think you win the "bad indentation" award for the day. Some of the worst I've seen.
                                  Listen, if you want folks to read your code, you'd be smart to make it easy to read.
                                  • 14. Re: read the first word of each line in a text file
                                    baftos
                                         String testy;
                                                                       thing[0]=testy;
                                                                       System.out.println(testy);
                                    I selected only the lines where testy is used. Can you tell where testy gets assigned a value?
                                    1 2 Previous Next