找不到符号 charAt(int)?
Cannot find symbol charAt(int)?
有人可以告诉我我做错了什么吗?
我正在尝试访问地图并将以字母 "N" 开头的键放入数组中。但是,我收到一个 Cannot find symbol error referenced to charAt(int)
? Intellij 建议我为 chartAt
?
创建一个摘要 class
import java.util.Map;
public class RoadNetwork {
String[] nodeList;
public void storeNodes(Map<String, Element> result) {
int counter =0;
nodeList = new String[result.size()];
//Cycle through Map to find elements which are Nodes
for (int i = 0; i < result.size(); i++) {
//if Node, then add it to array
if (result.get(i).charAt(0) == "N") {
nodeList[i] = String.valueOf(result.get(i));
counter++;
}
}
System.out.println("Nodes Array Length" + counter);
}
}
似乎有问题
if (result.get(i).charAt(0) == "N") {
可能您想要检索键但是 get()
方法 returns 值,它是类型 Element
,没有方法 charAt()
。
您可以尝试类似的方法:
for (String key:result.keySet()) {
//if Node, then add it to array
if (key.charAt(0) == 'N') { //'N' and not "N"
nodeList[i] = String.valueOf(result.get(key));
counter++;
}
}
您的地图将键作为字符串,并且您在行中传递了 int
if (result.get(i).charAt(0) == "N") {
所以不是传递 result.get(int) 而是传递 result.get(String)
要检查从 N 开始的键,请执行以下操作:
整数计数器=0;
nodeList = new String[result.size()];
//Cycle through Map to find elements which are Nodes
int i = 0;
//if Node, then add it to array
for(String key : result.keySet())
{
if (key.charAt(0) == 'N') {
nodeList[i] = key;
counter++;
}
}
System.out.println("Nodes Array Length" + counter);
有人可以告诉我我做错了什么吗?
我正在尝试访问地图并将以字母 "N" 开头的键放入数组中。但是,我收到一个 Cannot find symbol error referenced to charAt(int)
? Intellij 建议我为 chartAt
?
import java.util.Map;
public class RoadNetwork {
String[] nodeList;
public void storeNodes(Map<String, Element> result) {
int counter =0;
nodeList = new String[result.size()];
//Cycle through Map to find elements which are Nodes
for (int i = 0; i < result.size(); i++) {
//if Node, then add it to array
if (result.get(i).charAt(0) == "N") {
nodeList[i] = String.valueOf(result.get(i));
counter++;
}
}
System.out.println("Nodes Array Length" + counter);
}
}
if (result.get(i).charAt(0) == "N") {
可能您想要检索键但是 get()
方法 returns 值,它是类型 Element
,没有方法 charAt()
。
您可以尝试类似的方法:
for (String key:result.keySet()) {
//if Node, then add it to array
if (key.charAt(0) == 'N') { //'N' and not "N"
nodeList[i] = String.valueOf(result.get(key));
counter++;
}
}
您的地图将键作为字符串,并且您在行中传递了 int if (result.get(i).charAt(0) == "N") { 所以不是传递 result.get(int) 而是传递 result.get(String)
要检查从 N 开始的键,请执行以下操作:
整数计数器=0; nodeList = new String[result.size()];
//Cycle through Map to find elements which are Nodes
int i = 0;
//if Node, then add it to array
for(String key : result.keySet())
{
if (key.charAt(0) == 'N') {
nodeList[i] = key;
counter++;
}
}
System.out.println("Nodes Array Length" + counter);