我有一个程序从文件读取,获取每个单词并将其作为字符串添加到数组中。我在将字符串添加到数组时遇到了麻烦。我得到错误:
SortingWords.java:73: error: cannot find symbol
array.add(element);
^
符号:方法add(String)位置:String []类型的变量数组
我检查了我的拼写,一切似乎都还好。发生了什么事,我该如何解决?
public class SortingWords {
//global variables
public static int count = 0;
public static void main(String [ ] commandlineArguments){
String[ ] array = readFileReturnWords(commandlineArguments[0]);
sortAndPrintArray(array, commandlineArguments[0]);
if (commandlineArguments.length != 1) {
System.out.println("Please enter the INPUT file name as the 1st commandline argument.");
System.out.println("Please enter exactly one (1) commandline arguments.");
// Immediately terminates program
System.exit(1);
}// end of if
// if no commandline argument errors, continue program
String inputFile = commandlineArguments[0];
}
/**
* readFileReturnWords - Used To Read from File & Store Each Word in Array
*
* @param inputFile is the name of the file
*/
public static String [] readFileReturnWords(String inputFile){
//create array
//read one word at a time from file and store in array
//return the array
//error checking for commandline input
//make an array of Strings
final Integer MAX = 100;
String array[] = new String [MAX];
String element = "";
// read items from file & store in array
//connects to file
File file = new File(inputFile);
Scanner scanFile = null;
try {
scanFile = new Scanner(file);
}
catch (FileNotFoundException exception) {
// Print error message.
System.out.print("ERROR: File not found for " + inputFile);
}
// if made connection to file, read from file
if (scanFile != null) {
String firstLineOfFile = "";
firstLineOfFile = scanFile.nextLine();
// keeps looping if file has more lines..
while (scanFile.hasNextLine()) {
// get a line of text..
String line = scanFile.nextLine();
// divides each line by commas
Scanner lineInput = new Scanner(line).useDelimiter("\\s*");
element = lineInput.next();
array.add(element);
count ++;
}
}
System.out.println("Alphabetical (ASCII) listing of words in file: ");
System.out.println("Index Element");
for(int i=0;i<MAX;i++){
System.out.print("Index = " );
System.out.printf("%-7d", i);
System.out.print(", Element = ");
System.out.println(array[i]);
}
return array;
}
非常感谢
答案很长很无聊,如果您只想简单回答,请跳到底部 。
好的,伙计,我想我在这里看到您的问题,首先我要说的是,您可能会混淆STRING
LISTS(请注意,不要使混淆的列表变得非常复杂,并且我要让我的硕士参加整个课程列表D =)上的字符串“ STRING ARRAYS”。
列表是一个可以放入各种东西的袋子,例如食品杂货袋,您可以放入胡萝卜,豌豆,苹果,或者在我们的情况下,当您使用Java字符串,整数等进行编程时…在袋子里的东西没有顺序。数组更像是奥利奥Cookie容器(数组)。您只将奥利奥(Oreos)放在那儿,它们都放入槽中并停留在那里,这与食品杂货袋(清单)不同,在杂物袋中,物品可能掉落到底部或顶部…。
这在数组中很重要,您无法更改大小,因此无法执行
array.add(element)
如果您发现自己在问为什么,那么您需要考虑一下。如果创建一个包含2个元素的数组怎么办?那么每个元素都去了哪里?即使您不理解,Java语言也要求在Array中指定对象的位置。因此,要将对象添加到数组中,您需要指定位置。然后将其设置为想要的w
/ e,例如
那个地点,
array[0];
简单回答 您将其设置为等于
array[0] = "I want it to equal this string!!!";
简单答案的结尾
现在让我们看一下购物袋(它没有像数组一样的“槽”),请注意,您的代码中似乎没有列表,
List<String> myBrandNewShinyList= new ArrayList<String>();
制作完此列表之后,您就可以像这样使用.add()了,
myBrandNewShinyList.add("Let's add this string!!!");
现在您知道其中的好运了。我也犯了同样多次的错误…
这里有一个类似的答案:如何在Java中将函数作为参数传递? 但提供的正确答案不起作用。我有一门课: 在函数内部我试图将传递到,但我得到的错误是: 找不到符号 符号:类Callable 我不知道为什么。 另外,我尝试使用返回类型字符串作为xMethod,您能传递一个返回类型不同的函数吗?
问题内容: 我使用时代码工作正常 吗?在我看来这很奇怪。 (错误显示在终端上) 问题答案: 这是 Arrays 类的静态方法。 您应该像这样调用它: 请注意,您仍然必须像这样导入Arrays类: 或者像其他人提到的那样,如果您进行静态导入,则可以省略类名。 我认为这样做对可读性更好。
(菜鸟失误,我确定。) 我是计算机科学专业的一年级学生,正在尝试用代码编写作业程序; 但是在编译时,我得到了错误; Lab10Ex1.java:10: 错误: 找不到符号 System.out.print(n 1);^ 符号:变量n 位置: 班级 Lab10Ex1 如果有人能向我解释我做错了什么,或者如何纠正它,我会非常感激。
我有一个问题,我试图实现一个JMenuBar,但不断得到一个不能找到符号错误。我将JFrame与main方法一起放在一个单独的类中,JFrame将安装MainPanel类的一个对象。我在这里漏掉了什么?如有任何帮助将不胜感激,谢谢! 符号:方法setJMenuBar(JMenuBar) 位置:类主面板
问题内容: 当代码到达递归调用增量时,我收到错误找不到符号,我不知道为什么?这是增量代码。任何帮助将不胜感激。 编辑:我真的是Java的新手,所以可以做的答案越基本越好。好的,所以我收到的错误是:BigNatural.java.35:找不到符号符号方法增量()位置:类java.lang.String temp.increment() 并在此处清除所有其他问题,是整个代码。 }公共类BigNatur
我下载了一个Android项目,但我得到了这个错误: 样品导入: 样品用法: 代码时没有错误,但在编译时我得到了我提到的错误。 当我尝试在Android Studio上进行定义时,我不能。 应用程序级生成。格拉德尔: 我试着清理和重建这个项目,但还没有成功。 谢谢你的帮助。