2016-02-27 76 views
-1

我有一个程序从文件中读取,获取每个单词并将其作为字符串添加到数组中。将字符串添加到数组中有点麻烦。 我得到的错误:错误:找不到符号array.add(element);

SortingWords.java:73: error: cannot find symbol 
     array.add(element); 
      ^

符号:方法Add(字符串) 位置:类型的变量数组的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; 
} 

,不胜感谢

+0

检查数组的方法 – Musaddique

回答

2

很长的无聊的回答,跳到底部,如果你只是想简单的答案 ...

好的好友我想我在这里看到你的问题,我会开始说你可能会混淆STRING LISTS(注意不要弄混淆列表会变得非常复杂,而且我会让我的主人有一整个班级名单D =)与STRING ARRAYS。

列表是一个袋子,你把各种东西放在一起,就像一个杂货袋,你可以放在胡萝卜,豌豆,苹果或在我们的情况下编程时在Java字符串,整数等...在一个袋子的东西做没有订单。数组更像一个奥利奥饼干容器(数组)。你只把oreos放在那里,他们都进入一个插槽并留在那里,这不同于一个杂货袋(列表),其中一件物品可能落到底部或顶部....

这是一个数组重要的,如果你发现自己在问,为什么那么你需要想想你不能改变大小,所以你不能做

array.add(element) 

。如果你制作一个包含2个元素的数组呢?那么每个元素都去哪里?即使你不理解它,Java语言也要求在数组中指定对象的位置。因此,要将对象添加到数组中,您需要指定位置。然后将其设置为你想要例如W/E,

位置,

array[0]; 

答案很简单 什么要设置它等于

array[0] = "I want it to equal this string!!!"; 

完简单的回答

现在让我们看看食品袋(它没有“插槽”像数组),请注意,它看起来像有在你的代码中没有列表,

List<String> myBrandNewShinyList= new ArrayList<String>(); 

一旦你取得了这个名单,那么你可以使用你使用像这样的。新增(),

myBrandNewShinyList.add("Let's add this string!!!"); 

现在你知道不同的祝你好运。我也犯过同样的错误太多了......

+0

哇。我希望更多的教科书/教授像这样破坏事情。使它更易于理解和记忆。谢谢! – EmsandEms

+0

乐于助人。 IMO编程非常简单,只是很难找到正确/简单的答案。 –

2

你感到困惑与阵列和ArrayList

数组没有add方法,它们的插入会根据指标

array[index] = value;

与之相似,他们只能用指数

被retrived
value = arrray[index]; 
+0

好的,谢谢。更改为数组[count] = element;现在编译。 – EmsandEms

1

使用ArrayList而不是阵列:

List<String> array = new ArrayList<String>(); 

while (scanFile.hasNextLine()) { 
    String line = scanFile.nextLine(); 
    Scanner lineInput = new Scanner(line).useDelimiter("\\s*"); 
    element = lineInput.next(); 

    // the add() method is available for Collections but 
    // not for primitive arrays 
    array.add(element); 
    count++; 
} 

for (String element : array) { 
    System.out.print("Index = "); 
    System.out.printf("%-7d", i); 
    System.out.print(", Element = "); 
    System.out.println(element); 
} 
1

java中的数组有一个固定的插槽数。你必须知道在你使用阵列时你需要的数字。

你想要的是两种:

array[count] = element; 

,或者你可以使用一个List<String>(例如ArrayList<String>,因为List只是一个interface

+0

另外,如果您想从另一个文件中读取,则使用全局变量可能会成为一个问题。对于每行大于100行的文件,数组的静态长度将产生一个ArrayIndexOutOfBoundsException异常 –

0

这是不是你的元素添加到Java数组。保留一个计数器,将循环块里面增加外循环,然后添加元素,如下面:

int i = 0; 
while(.....){ 
    . 
    . 
    array[i] = //element to be added 
    i++; 
}