【问题标题】:using split on Array Object in Java在 Java 中对数组对象使用拆分
【发布时间】:2015-03-05 22:08:36
【问题描述】:

当我在字符串对象上使用 .split 时,为什么会收到带有“找不到符号”的 mssg?

public static void main(String[] args) {


String[] inputFile = StdIn.readAllLines().split("\\n");

//create Congress caracteristics
int states = Integer.parseInt(inputFile[0]);        // read states value and save in the first 
//position in the array

int totalSeats = Integer.parseInt(inputFile[1]);   //read number of seats and save in the second
//position in the array

【问题讨论】:

标签: java arrays string object split


【解决方案1】:

这是因为您试图在数组上调用 split 方法。数组上没有这样的方法。 readAllLines() 已经返回了一个字符串数组。只需删除 .split() 调用即可。

来自JavaDoc

static String[] readAllLines()

从标准输入读取所有剩余的行并将它们作为字符串数组返回。

【讨论】:

    【解决方案2】:

    StdIn.readAllLines() 不返回字符串,而是返回 String[](字符串数组)。您不能在字符串数组上使用split()

    实际上,您不需要这样做,因为调用 readAllLines() 已经为您提供了您正在寻找的数组。

    【讨论】:

      【解决方案3】:

      您很可能在字符串数组/集合中调用#split(),而不是字符串。您应该遍历它们并一个接一个地拆分。

      【讨论】:

        【解决方案4】:
           We have fileName Object we itrate it a split it simple  in this program i split my object with (.) so thats why i used this =split("\\.");
        
        
        
            String[] fileName="we have a object here to we want to splite it ";
        
        
            for(String obj:FileName)
                    {
                String[] part1=obj.split("\\.");
                    }
            System.out.println(part1);
        
        
        
            check it its working fine 
        

        【讨论】:

          猜你喜欢
          • 2022-01-22
          • 2018-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-04
          • 1970-01-01
          • 2019-04-15
          相关资源
          最近更新 更多