2016-02-25 29 views
-1

我正在處理的程序應該將來自掃描儀的值從不同的方法添加到它們各自的數組中。然而,當使用代碼exampleArray.push(value)時,它給了我一個錯誤:「Error:can not find symbol,symbol:method push(java.lang.String)。」 我做錯了什麼?我錯過了什麼? 該課程的代碼:不適用於我的代碼,是否有某處存在錯誤或者我錯誤地使用它?

public class Agenda 
{ 
    public static String names[]; 
    public static int days[]; 
    public static int types[]; 
    public static int diffs[]; 
    public static int row; 
    public Agenda() 
    { 
    } 
    public void createA() throws IOException 
{ 
    FileWriter nFile = new FileWriter("CurrentAgenda.txt", false); 
    PrintWriter output = new PrintWriter(nFile, false); 
    output.println("Current Agenda"); 
    //clear out data file 

    output.close(); 
    nFile.close(); 

    FileWriter dFile = new FileWriter("Data.txt", false); 
    PrintWriter clear = new PrintWriter(dFile, false); 
    clear.print(""); 
    clear.close(); 
    dFile.close(); 
    create(); 
    } 
    public static void create() throws IOException 
    { 
    System.out.println("Your file will be: CurrentAgenda.txt"); 
    System.out.println("Input types:"); 
    System.out.println("Name--> Characters: A-Z"); 
    System.out.println("Type/Summative=(3), Formative=(2), Extra=(1)--> Integer: 1-3"); 
    System.out.println("Days Until Deadline--> Integer: Any"); 
    System.out.println("Difficulty/Easy=(1), Medium=(2), Hard=(3)--> Integer: 1-3"); 
    System.out.println("Please input assignments:\n"); 
    String again; 

    names = new String[100]; 
    days = new int[100]; 
    types = new int[100]; 
    diffs = new int[100]; 
    row=0; 
    do{ 
     FileWriter data = new FileWriter("Data.txt", true); //putting data in table for append 
     PrintWriter append = new PrintWriter(data, true); 

     Scanner input = new Scanner(System.in); 

     System.out.println("Please input assignment name: "); 
     String name = input.nextLine(); 
     names[row] = name; 
     append.print(name +","); 

     System.out.println("Please input the number of days until the due date: "); 
     int day = input.nextInt(); 
     days[row] = day; 
     append.print(day + ","); 

     System.out.println("Please input assignment type:"); 
     int type = input.nextInt(); 
     types[row] = type; 
     append.print(type + ","); 

     System.out.println("Please input the assignment difficulty: "); 
     int diff = input.nextInt(); 
     diffs[row] = diff; 
     append.println(diff); 

     append.close(); 
     data.close(); 

     System.out.println("Would you like to add another item? Enter 'Yes' or 'No'"); 
     again = input.next(); 
     input.close(); 
     row++; 

    } 
    while(again.equalsIgnoreCase("Yes")); 

    List<Items> work = new ArrayList<Items>(); 
    for(int count = 0; count<row; count++) 
    { 
     work.add(new Items((names[count]),(days[count]),(types[count]),(diffs[count]))); 
    } 
    Collections.sort(work, new Comp1()); 

    FileWriter firstL = new FileWriter("CurrentAgenda.txt", true); //formats the Viewer file 
    PrintWriter paste = new PrintWriter(firstL, true); 
    paste.println("Do these Assignments in order:"); 
    paste.close(); 
    firstL.close(); 

    System.out.println("Sorted Assignment Entries: "); 

    for(Items e:work) 
    { 
     FileWriter agenda = new FileWriter("CurrentAgenda.txt", true); 
     PrintWriter add = new PrintWriter(agenda, true); 
     add.println(e); 
     add.close(); 
     agenda.close(); 
     System.out.println(e); 
    } 
    } 

    public static void add() throws IOException 
    { 
    String again; 
    do{ 
     FileWriter data = new FileWriter("Data.txt", true); 
     PrintWriter append = new PrintWriter(data, true); 

     Scanner input = new Scanner(System.in); 

     System.out.println("Please input assignment name: "); 
     String name = input.nextLine(); 
     names.push(name); 
     append.print(name +","); 

     System.out.println("Please input the number of days until the due date: "); 
     int day = input.nextInt(); 
     days.push(day); 
     append.print(day + ","); 

     System.out.println("Please input assignment type:"); 
     int type = input.nextInt(); 
     types.push(type); 
     append.print(type + ","); 

     System.out.println("Please input the assignment difficulty: "); 
     int diff = input.nextInt(); 
     diffs.push(diff); 
     append.println(diff); 

     append.close(); 
     data.close(); 

     System.out.println("Would you like to add another item? Enter 'Yes' or 'No'"); 
     again = input.next(); 
     ++row; 

    } 
    while(again.equalsIgnoreCase("Yes")); 


    List<Items> work2 = new ArrayList<Items>(); 
    for(int count = 0; count<row; count++) 
    { 
     work2.add(new Items((names[count]),(days[count]),(types[count]),(diffs[count]))); 
    } 

    Collections.sort(work2, new Comp1()); 

    FileWriter firstL = new FileWriter("CurrentAgenda.txt", false); 
    PrintWriter paste = new PrintWriter(firstL, false); 
    paste.println("Current Agenda"); 
    paste.println("Do these Assignments in order:"); 
    paste.close(); 
    firstL.close(); 

    System.out.println("Sorted Assignment Entries: "); 

    for(Items e:work2) 
    { 
     FileWriter agenda = new FileWriter("CurrentAgenda.txt", true); 
     PrintWriter add = new PrintWriter(agenda, true); 
     add.println(e); 
     add.close(); 
     agenda.close(); 
     System.out.println(e); 
    } 
    } 
+0

有沒有'推()'的'Array'對象定義的方法在Java中。 –

+0

你有任何特定的要求使用數組over ArrayList? – Suyash

+0

我正在使用一個比較器,按照它們的順序對4個值進行排序和對象,所以我決定使用一個數組列表。我沒有在這裏輸入物品類代碼對不起 –

回答

1

java陣列沒有push方法。相反,您必須通過使用您要設置它的索引來設置值。例如,您可以通過說:names[0]="Rob";將名稱數組的第一個點設置爲Rob。您將必須知道索引。對於您的情況,您可以考慮使用從0開始的計數器,並且每次保存作業名稱時都會遞增。

希望這會有所幫助!

+0

這很奇怪,因爲當我查找如何追加到數組這就是出現了什麼。 –

+0

是的,這就是我以前想做的事情,儘管我一直在爲此發生空點異常錯誤。我會再嘗試! –

+0

@NatalieB你得到空指針異常的原因是因爲names是一個String數組。由於字符串是對象,數組中每個點的默認值爲空。嘗試使用for循環遍歷整個循環,並將值設置爲「」(空字符串)或類似的東西。 – FlamingPickle

1

您不應該使用標準陣列。你的編譯器是正確的,沒有push方法的標準數組。

但是您可以使用不同的數組類型。

嘗試使用ArrayDeque(文檔here)。它有你正在尋找的推送方法。

你只需要記住,它們的初始化與數組有一點不同。

而不是

String[] array = new String[50]; 

您使用

ArrayDeque<String> array = new ArrayDeque<>(50); 

的構造函數的能力參數可選。與標準陣列不同,ArrayDeque的大小可以改變。

然後,您可以調用您的push(E)方法,就像您在代碼中一樣。

+0

我也會檢查一下! –

0

一個push()方法不存在數組,如果你願意,你可以做一個通用梅託德推送到你的陣列是這樣的:

<T> void push(T[] array, int index, T value) { 
     array[index] = value; 
} 
相關問題