我正在處理的程序應該將來自掃描儀的值從不同的方法添加到它們各自的數組中。然而,當使用代碼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);
}
}
有沒有'推()'的'Array'對象定義的方法在Java中。 –
你有任何特定的要求使用數組over ArrayList? – Suyash
我正在使用一個比較器,按照它們的順序對4個值進行排序和對象,所以我決定使用一個數組列表。我沒有在這裏輸入物品類代碼對不起 –