java - String[] 数组上的 .split 和 .indexOf

标签 java

我之前的问题:Java - Importing text file into array when lines are not consistent

每次我尝试使用 .split 或 .indexOf 时,都会收到一条错误消息:“无法在数组类型 String[] 上调用 split(String, int)”。 Eclipse 没有太大帮助,建议我将其更改为 .length

我的代码:

import java.util.*;
import java.io.*;
public class Club 
{
Scanner ConsoleInput;
public int count;
public Club() throws IOException
{
    String clubtxt = ("NRLclubs.txt");
    int i;

    File clubfile = new File(clubtxt);

    if (clubfile.exists())
    {
        count = 0;
        Scanner inputFile = new Scanner(clubfile);
        i = 0;
        while(inputFile.hasNextLine())
        {
            count++;
            inputFile.nextLine();
        }
        String[] teamclub = new String[count];
        inputFile.close();
        inputFile = new Scanner(clubfile);
        while(inputFile.hasNext())
        {
            teamclub[i] = inputFile.nextLine();
            System.out.println(teamclub[i]);
            i++;
        }
        inputFile.close();
        SplitClubdata(teamclub, count);
    }
    else
    {
        System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n");
    }

}
public void SplitClubdata(String[] teamclub, int count)
{
    String[] line = teamclub;
    int maxlines = count;
    count = 0;

            while(count <= maxlines)
            {
            // Split on commas but only make three elements
            String elements[] = line.split(",", 3);


            String names[] = new String[maxlines];
            String mascot[] = new String[maxlines];
            String aliases[] = new String[maxlines];
            // The first belongs to names
            names[count] = elements[0];
            // The second belongs to mascot
            mascot[count] = elements[1];
            // And the last belongs to aliases
            aliases[count] = elements[2];
            count++;
            }
}
}

有人知道如何解决这个问题吗?

最佳答案

lineString数组,您无法调用split在上面。我认为你的意思是 line[count].split(",", 3) .

我还建议重组此类并使用适当的技术:

  • 不要读取文件两次来获取 count .
  • 使用 ArrayList<Club>其中 Club 具有字段( mascotnamealias )。

这是一个更清晰的版本:

package org.argaus.gwt.tls.portlet;

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

public class Club {

    private String name;
    private String mascot;
    private String alias;

    public Club(String name, String mascot, String alias) {
        this.name = name;
        this.mascot = mascot;
        this.alias = alias;
    }

    public static List<Club> ReadClubsFromFile() throws IOException {


        File clubfile = new File("NRLclubs.txt");
        List<Club> clubs = new ArrayList<Club>();
        if (clubfile.canRead()) {
            Scanner inputFile = new Scanner(clubfile);
            inputFile = new Scanner(clubfile);
            while (inputFile.hasNext()) {
                String[] parts = inputFile.nextLine().split(",", 3);
                clubs.add(new Club(parts[0], parts[1], parts[2]));

            }
            inputFile.close();
        }
        else {
            System.out.println("\n" + "The file " + clubfile + " does not exist." + "\n");
        }
        return clubs;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMascot() {
        return mascot;
    }

    public void setMascot(String mascot) {
        this.mascot = mascot;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }
}

关于java - String[] 数组上的 .split 和 .indexOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832613/

相关文章:

Java 中 COM 的等价物

java - 在多个类中共享 Java 中的常量字符串?

java - "No X11 DISPLAY variable"- 这是什么意思?

Java Swing 使用 defaulttablemodel 根据索引设置行和列的颜色

java - 重复循环大量对象时如何优化性能

mysql - 非静态...静态什么?

java - 即使我将 boolean 值设置为自动为假,如果语句也不起作用

用于根据特定字段对对象列表进行排序的 java 8 流

java - 使用 Guice 为可重用组件注入(inject)不同的实例

java - Dijkstra算法中使用的优先级队列的比较器类实现?