gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:16 25 4
gpt4 key购买 nike

我之前的问题: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com