首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用三个字分割字符串

用三个字分割字符串
EN

Stack Overflow用户
提问于 2014-03-05 16:46:57
回答 4查看 190关注 0票数 0

拆分包含三个单词的字符串的最佳方法是什么?

我的代码现在看起来就像这样(更新的代码见下面):

代码语言:javascript
复制
BufferedReader infile = new BufferedReader(new FileReader("file.txt"));

        String line;
        int i = 0;

        while ((line = infile.readLine()) != null) {
            String first, second, last;
            //Split line into first, second and last (word)

            //Do something with words (no help needed)
            i++;
        }

下面是完整的file.txt:

Allegrettho Albert 0111-27543 Brio Britta 0113-45771 Cresendo Crister 0111-27440 Dacapo Dan 0111-90519 Dolce Dolly 0116-31418 Espressivo Eskil 0116-19042 Fortissimo Folke 0118-37547 Galanto Gunnel 0112-61805 Glissando Gloria 0112-43918 Grazioso格雷斯0112-43509 Hysterico Hilding 0119-71296 Interludio Inga 0116-22709 Jubilato Johan 0111-47678 Kverulando Kajsa 0119-34995 Legato Lasse 0116-26995 Majestoso Maja 0116-80308 Marcato Maria 0113-25788 Molto Maja 0117-91490 Nontroppo0119-12663专用的Osvald 0112-75541 Parlando Palle 0112-84460钢琴Pia 0111-10729马铃薯Putte 0112-61412 Presto Pelle 0113-54895 Ritardando Rita 0117-20295 Staccato Stina 0112-12107 Subito Sune 0111-37574节拍Kalle 0114-95968 Unisono Uno 0113-16714 Virtuoso Vilhelm 0114-10931 Xelerando Axel 0113-89124

@Pshemo建议的新代码:

代码语言:javascript
复制
public String load() {
    try {
        Scanner scanner = new Scanner(new File("reg.txt"));

        while (scanner.hasNextLine()) {
            String firstname = scanner.next();
            String lastname = scanner.next();
            String number = scanner.next();
            list.add(new Entry(firstname, lastname, number));
        }
        msg = "The file reg.txt has been opened";
        return msg;
    } catch (NumberFormatException ne) {
            msg = ("Can't find reg.txt");
            return msg;
    } catch (IOException ie) {
            msg = ("Can't find reg.txt");
            return msg;
    }
}

我收到多个错误,怎么了?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-03-05 17:01:23

假设每行总是包含三个单词而不是拆分,那么您可以对每一行使用Scanner的方法next三次。

代码语言:javascript
复制
Scanner scanner = new Scanner(new File("file.txt"));
int i = 0;
while (scanner.hasNextLine()) {
    String first = scanner.next();
    String second = scanner.next();
    String last = scanner.next();
    //System.out.println(first+": "+second+": "+last);
    i++;
}
票数 1
EN

Stack Overflow用户

发布于 2014-03-05 16:51:29

代码语言:javascript
复制
line.split("\\s+"); // don't use " ". use "\\s+" for more than one whitespace
票数 0
EN

Stack Overflow用户

发布于 2014-03-05 16:52:11

假设这一行有3+单词,使用split(delimiter)方法:

代码语言:javascript
复制
String line = ...;

String[] parts = line.split("\\s+"); // Assuming words are separated by whitespaces, use another if required

然后您可以分别访问第一、第二和最后一个:

代码语言:javascript
复制
String first = parts[0];
String second = parts[1];
String last = parts[parts.length() - 1];

记住,索引以0开头。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22203954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档