简体   繁体   English

Java Scanner多个定界符

[英]Java Scanner multiple delimiter

I know this is a very asked question but I can't find and apropiate answer for my problem. 我知道这是一个非常常见的问题,但是我找不到合适的答案。 Thing is I have to program and aplication that reads from a .TXT file like this 我必须编程和从这样的.TXT文件读取的应用程序

Real:Atelti
Alcorcon:getafe 
Barcelona:Sporting

My question is how what can I do to tell Java that I want String before : in one ArrayList and Strings after : in another ArrayList?? 我的问题是如何告诉Java我想要在一个ArrayList中的String之前和在另一个ArrayList中的Strings之后的Java? I guess It's using delimeter method but I don't know how use it in this case. 我猜它正在使用分度法,但在这种情况下我不知道如何使用。

Sorry for my poor english, I've to improve It i guess. 对不起,我的英语不好,我得提高一点。 Thanks 谢谢

use split function of java. 使用Java的split函数。

steps: 脚步:

  1. Declare two arrayList. 声明两个arrayList。 l1 and l2; l1和l2;
  2. read each line. 阅读每一行。
  3. split each line by ":", this will return a array of length 2, array. 用“:”分隔每行,这将返回一个长度为2的数组array。 (as per your input) (根据您的输入)
  4. l1.add(array[0]) , l2.add(array 1 ) l1.add(array [0]),l2.add(array 1

try yourself, post code if you need help :) 试试看,如果需要帮助,请发布代码:)

check here for use of split function, though through google you can find many different example 检查这里使用拆分功能,尽管通过谷歌您可以找到许多不同的例子

Split the string using ":" as delimiter. 使用“:”作为分隔符分割字符串。 Add the odd entries from the result to one list and even to another list. 将结果中的奇数项添加到一个列表,甚至添加到另一列表。

If your text is like this: 如果您的文字是这样的:

Real:Atelti 真实:Atelti

Alcorcon:getafe Alcorcon:赫塔菲

Barcelona:Sporting 巴塞罗那:运动

You can achieve what you want by using: 您可以使用以下方法实现所需的功能:

StringBuilder text = new StringBuilder();
Scanner scanner = new Scanner(new FileInputStream(fFileName), encoding); //try utf8 or utf-8 for 'encoding'
try {
  while (scanner.hasNextLine()){
    String line = scanner.nextLine();
    String before = line.split(":")[0];
    String after = line.split(":")[1];
    //dsw 'before' and 'after' - add them to lists.
  }
}
finally{
  scanner.close();
}
        Scanner scanner = new Scanner(new FileInputStream("YOUR_FILE_PATH"));
        List<String> firstList = new ArrayList<String>();
        List<String> secondList = new ArrayList<String>();
        while(scanner.hasNextLine()) {
            String currentLine = scanner.nextLine();
            String[] tokenizedString = currentLine.split(":");
            firstList.add(tokenizedString[0]);
            secondList.add(tokenizedString[1]);
        }
              scanner.close();

Enumerating firstList and secondList will get you the desired result. 枚举firstList和secondList将为您提供所需的结果。

1. Use ":" as delimiter. 1.使用":"作为分隔符。

2. Then Store them in the String[] using split() function. 2.然后使用split()函数split()它们存储在String[]

3. Try using BufferedReader instead of Scanner. 3.尝试使用BufferedReader而不是Scanner。

Eg: 例如:

File f = new File("d:\\Mytext.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
ArrayList<String> s1 = new ArrayList<String>();
ArrayList<String> s2 = new ArrayList<String>();

  while ((br.readLine())!=null){

    String line = br.readLine();

    String bf = line.split(":")[0];
    String af = line.split(":")[1];

    s1.add(bf);
    s2.add(af);

  }

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

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