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

时间:2013-05-30 09:34:40

标签: 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++;
            }
}
}

有人对如何解决这个问题有任何想法吗?

4 个答案:

答案 0 :(得分:1)

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

我还建议重组这个课程并使用适当的技巧:

  • 请勿两次阅读文件以获取count
  • 使用ArrayList<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;
    }
}

答案 1 :(得分:0)

split在String对象上调用,而不是在字符串数组上调用。

Probabaly这是你应该做的:

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

而不是

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

答案 2 :(得分:0)

您正在尝试split()数组,您只能在String上调用这些方法:

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

答案 3 :(得分:0)

indexOf无法使用splitArrays方法。

可用于数组的字段/方法包括:

  

公共最终字段长度,包含组件数   数组。长度可以是正数或零。

     

公共方法clone,它覆盖同名方法   在类Object中并且不抛出任何已检查的异常。返回类型   数组类型T []的克隆方法是T []。

     

多维数组的克隆很浅,也就是说   它只创建一个新数组。子阵列是共享的。

     

所有成员都继承自Object类; Object的唯一方法   没有继承的是它的克隆方法。

请参阅Java Specification