Search code examples
javaiojava.util.scanner

How to read a column from a text file in java using scanner?


I have this text that I want to read the first column from in my project named Data.txt.

//column names:   productId, name,numInStock,provider,pricePerUnit

private static String records =     "231A,Light Bulb,123,Wilco,1.75:"+
                                "113D,Hairbrush,19,Aamco,3.75:"+
                                "521W,Shampoo,24,Acme,6.95:"+
                                "440Q,Dishwashing Detergent,20,Wilco,1.75:"+
                                "009G,Toothbrush,77,Wilco,0.85:"+
                                "336C,Comb,34,Wilco,0.99:"+
                                "523E,Paper Pad Set,109,Congdon and Chrome,2.45:"+
                                "888A,Fake Diamond Ring,111,Americus Diamond,3.95:"+
                                "176A,Romance Nove1 1,20,Barnes and Noble,3.50:"+
                                "176B,Romance Nove1 2,20,Barnes and Noble,3.50:"+
                                "176C,Romance Nove1 3,20,Barnes and Noble,3.50:"+
                                "500D,Floss,44,Wilco,1.25:"+
                                "135B,Ant Farm,5,Wilco,8.00:"+
                                "211Q,Bicycle,9,Schwinn,75.95:"+
                                "932V,Pen Set,50,Congdon and Chrome,9.95:"+
                                "678Q,Pencil 50,123,Congdon and Chrome,9.95:"+
                                "239A,Colored Pencils,25,Congdon and Chrome,4.75:"+
                                "975B,Shower Curtain,25,Wilco,6.50:"+
                                "870K,Dog Bowl,15,Wilco,4.75:"+
                                "231S,Cat Bowl,15,Wilco,4.75:"+
                                "562M,Kitty Litter,15,Wilco,3.25:"+
                                "777X,Dog Bone,15,Wilco,4.15:"+
                                "933W,Cat Toy,15,Wilco,2.35:"+
                                "215A,Hair Ball,0,Little Jimmy,0.00:";

I wrote this code to do so but I am not getting the desired output. My code is

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        Scanner input = new Scanner(System.in);

        File file = new File("C:\\Users\\student\\Desktop\\workspace\\Welcome\\src\\Data.txt");
        input = new Scanner(file);

        while (input.hasNextLine()) {
            String line = input.nextLine();
            String[] words = line.split(",");
            System.out.println(words[0]);
            input.useDelimiter(",");
        }
        input.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

I want to get the output in this format

               134A
               213A
               911C
               012E
               662Z

This is the output I got I want to remove the part that says private static String records = and also the ".

//column names:   productId

private static String records =     "231A
                                "113D
                                "521W
                                "440Q
                                "009G
                                "336C
                                "231S

Solution

  • You're sort-of on the right track with the delimeters. Here's how I did it and it can be cleaned up a bit, and you'll have to make a for loop to iterate through the arraylist and get each one, and split each one and print each one's 0'th index ( "str" is the string/text file you provided )

              Scanner in = new Scanner(str) ;
          in.useDelimiter(":") ;
          ArrayList<String> al = new ArrayList<>() ;
          while( in.hasNext())
          {
              al.add(in.next() ) ;
          }
          
          for( int i = 0 ; i < al.size(); ++i )
          {
              String s = al.get(i) ;
              String[] s2 = s.split(",") ;
              System.out.println( s2[0] );
          }