java.util.scanner - reading strings from text file using java -


col1 col2 col3 abc 22 rome xyz 11 spain rome 12 uae 

want read text file, every column values individual objects compare between 2 object of column.

here code, suggest proper modification

/*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */ package javaapplication3;  import java.util.*; import java.io.*;  public class javaapplication3 {      /**      * @param args command line arguments      */     public static void main(string[] args) throws filenotfoundexception {         try{         scanner s = new scanner(new filereader("exemple_1.txt"));          list<string> ls1 = new arraylist<string>(); /* store closedset values */         list<string> ls2 = new arraylist<string>(); /* store support values */          list<string> ls3 = new arraylist<string>(); /* store objectlist vaules */          while(s.hasnext()){             string[] str = s.next().split("\t");             string s1 = str[0].trim();      /* takes closedset vaues read */             string s2 = str[1].trim();      /* takes support vaues read */             string s3= str[2].trim();       /* takes objectlist vaues read */              if(!"col1".equals(s1)){ /* skip first line */                 ls1.add(s1);             }             if(!"col2".equals(s2)){     /* skip first line */                 ls2.add(s2);             }             if(!"col3".equals(s3)){ /* skip first line */                 ls3.add(s3);             }         }          system.out.println(ls2);        /* print support values */          }catch (filenotfoundexception e) {             e.printstacktrace();         } /* catch (ioexception ex) {             ex.printstacktrace();         }   */     }  } 

if want read file line line, use s.hasnextline() , s.nextline(). also, equals(...) case sensitive, you've used !"col1".equals(s1) instead of !"col1".equals(s1)


Comments