简体   繁体   English

如何使用扫描仪从 java 中的文本文件中读取列?

[英]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.我有这个文本,我想从名为 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.我为此编写了此代码,但没有得到所需的 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我想以这种格式获取 output

               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 ".这是我得到的 output 我想删除私有 static 字符串记录 = 以及“。

//column names:   productId

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

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 )这是我的做法,可以稍微清理一下,您必须创建一个 for 循环来遍历 arraylist 并获取每个,然后拆分每个并打印每个索引的第 0 个索引(“str”是您提供的字符串/文本文件)

          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] );
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM