【问题标题】:Error "Cannot find symbol method charAt(int)"?错误“找不到符号方法 charAt(int)”?
【发布时间】:2014-02-02 21:04:01
【问题描述】:
System.out.println("Enter a sentence to get it translated into Pig Latin: ");

String sentence=kb.nextLine();

String[] words = sentence.split(" ");

char a=words.charAt(0);

【问题讨论】:

  • wordsString[]。数组类型只有继承自Object 的方法,即。不是charAt(int)

标签: java split charat


【解决方案1】:

发生的情况是words 是一个数组,而charAt() 方法是为Strings 定义的。所以你想使用:

words[index].charAt(0)

相反,indexString 在数组中的位置。

【讨论】:

    【解决方案2】:

    words 是一个字符串数组。尝试使用单词[索引]。拆分后,子字符串将存储如下 -

    Enter = words[0]
    a = words[1]
    sentence = words[2]
    to = words[3]
    get = words[4]
    it = words[5]
    translated = words[6]
    into = words[7]
    Pig = words[8]
    Latin: = words[9]
    

    访问 words[index].chartAt(0) 将返回相应子字符串的第一个字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 2018-01-23
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多