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

want to read this text file, every column values as individual objects to compare between any two object of those column.

here is my code, suggest some proper modification

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

import java.util.*;
import java.io.*;

public class JavaApplication3 {

    /**
     * @param args the 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>(); /* to store ClosedSet values */
        List<String> ls2 = new ArrayList<String>(); /* to store Support values */ 
        List<String> ls3 = new ArrayList<String>(); /* to store ObjectList vaules */

        while(s.hasNext()){
            String[] str = s.next().split("\t");
            String s1 = str[0].trim();      /* takes ClosedSet vaues to read */
            String s2 = str[1].trim();      /* takes Support vaues to read */
            String s3= str[2].trim();       /* takes ObjectList vaues to 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();
        }   */
    }

}
user207421
  • 289,834
  • 37
  • 266
  • 440
  • What do you think `s.next()` (in `s.next().split("\t")`) does? Also what is your question/problem you are facing? – Pshemo Mar 26 '16 at 14:01
  • reading elements to find the particular character "\t" for splitting.and the code shows some runtime error while executing – user6117629 Mar 26 '16 at 14:08
  • Which part of documentation of that method makes you think so? – Pshemo Mar 26 '16 at 14:09
  • Also "shows some runtime error while executing" doesn't help us help you. You need to be more precise (I suspect some kind of index-out-of-bounds exception, in which case you should go back to my first comment). – Pshemo Mar 26 '16 at 14:11
  • throws IOException in scanner class object most probably – user6117629 Mar 26 '16 at 14:12
  • [edit] your question and include stacktrace. – Pshemo Mar 26 '16 at 14:13

1 Answers1

0

If you want to read the file line by line, use s.hasNextLine() and s.nextLine(). Also, equals(...) is case sensitive, you've used !"Col1".equals(s1) instead of !"col1".equals(s1)

Titus
  • 20,544
  • 1
  • 19
  • 29