我为这个项目回收了一些旧代码,但我仔细检查了为这个项目重新定义了所有整数和字符串。你能让我知道我做错了什么吗?我不擅长编程。我得到的错误包括'sort','display'和'print'不能应用于(java.lang.String[],int[],java.io,BufferedReader
)。任何帮助将不胜感激。
编辑:这是stacktrace
C:\java\bin>javac CourseOrganizer.java
CourseOrganizer.java:23: getdata(int[],java.lang.String[],int) in CourseOrganize
r cannot be applied to (int[],java.lang.String[])
getdata(Grade,Course);
^
CourseOrganizer.java:28: cannot resolve symbol
symbol : method display (int[],java.lang.String[],java.io.BufferedReader)
location: class CourseOrganizer
display(Grade,Course,in);
^
CourseOrganizer.java:34: sort(int[],java.lang.String[],int,java.io.BufferedReade
r) in CourseOrganizer cannot be applied to (int[],java.lang.String[],java.io.Buf
feredReader)
sorttype=sort(Grade,Course,in);
^
CourseOrganizer.java:87: sortstring(int[],java.lang.String[],int) in CourseOrgan
izer cannot be applied to (int[],java.lang.String[])
{sortstring(Grade,Course);
^
CourseOrganizer.java:88: print(int[],java.lang.String[],int,java.io.BufferedRead
er) in CourseOrganizer cannot be applied to (int[],java.lang.String[],java.io.Bu
fferedReader)
print(Grade,Course,kb);
^
CourseOrganizer.java:92: sortint(int[],java.lang.String[],int) in CourseOrganize
r cannot be applied to (int[],java.lang.String[])
{sortint(Grade,Course);
^
CourseOrganizer.java:93: print(int[],java.lang.String[],int,java.io.BufferedRead
er) in CourseOrganizer cannot be applied to (int[],java.lang.String[],java.io.Bu
fferedReader)
print(Grade,Course,kb);
^
7 errors
import java.io.*;
import java.util.StringTokenizer;
class CourseOrganizer {
public static void main(String args[]) throws Exception
{
InputStreamReader kb = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(kb);
String buffer;
char choice;
boolean fileread=false;
int quantity=8;
int[]Grade=new int[quantity];
String[]Course=new String[quantity];
String sorttype="";
do
//Setup Menu
{choice=menu(in);
if(choice=='E')
{if(fileread)
System.out.println("Data already has been entered");
else
{fileread=true;
getdata(Grade,Course);
}
}
else if(choice=='D')
{if(fileread)
display(Grade,Course,in);
else
System.out.println("Must enter data before it is displayed");
}
else if(choice=='S')
{if(fileread)
sorttype=sort(Grade,Course,in);
else
System.out.println("Must enter data before it is sorted");
}
}while(choice!='X');
}
//Sort Data
public static void sortint(int Grade[],String Course[],int quantity)
{int i,j;
for(i=0;i<quantity-1;i++)
for(j=i+1;j<quantity;j++)
if(Grade[i]>Grade[j])
{swap(Grade,i,j);
swap(Course,i,j);
}
}
public static void sortstring(int Grade[],String Course[],int quantity)
{int i,j;
for(i=0;i<quantity-1;i++)
for(j=i+1;j<quantity;j++)
if(Course[i].compareToIgnoreCase(Course[j])>0)
{swap(Grade,i,j);
swap(Course,i,j);
}
}
public static void swap(int[] a,int i,int j)
{int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
public static void swap(String[] a,int i,int j)
{String t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
public static String sort(int Grade[],String Course[],int quantity,BufferedReader kb)throws Exception
{String buffer;
do
{
//Allow user to sort the criminals
System.out.println("What do you want to sort by?");
System.out.println("Course");
System.out.println("Grade");
System.out.print("Enter>>");
buffer=kb.readLine();
if(buffer.equalsIgnoreCase("Course"))
{sortstring(Course,Grade);
print(Course,Grade,kb);
return buffer;
}
else if(buffer.equalsIgnoreCase("Grade"))
{sortint(Course,Grade);
print(Course,Grade,kb);
return buffer;
}
System.out.println("Invalid entry");
}while(true);
}
public static String gettype(BufferedReader kb)throws Exception
{String buffer;
do
{
//Allow users to search for course
System.out.println("What course would you like to see?");
System.out.print("Enter>>");
buffer=kb.readLine();
if(buffer.equalsIgnoreCase("Calculus")||buffer.equalsIgnoreCase("Advanced Functions")||buffer.equalsIgnoreCase("Computer Science")||buffer.equalsIgnoreCase("Physics")||buffer.equalsIgnoreCase("Biology")||buffer.equalsIgnoreCase("Chemistry")||buffer.equalsIgnoreCase("History")||buffer.equalsIgnoreCase("English")||buffer.equalsIgnoreCase("Economics")||buffer.equalsIgnoreCase("Data Management"))
return buffer;
System.out.println("Invalid entry");
}while(true);
}
public static void print(int Grade[],String Course[],int quantity,BufferedReader kb)throws Exception
{System.out.println("Sorted data");
System.out.println("Grade\tCourse");
for(int i=0;i<quantity;i++)
System.out.println(Grade[i]+"\t"+Course[i]);
}
//Load criminal text file
public static void getdata(int Grade[],String Course[],int quantity)throws Exception
{FileReader file = new FileReader("courses.txt");
BufferedReader input = new BufferedReader(file);
int i;
String buffer;
for( i=0;i<quantity;i++)
{buffer=input.readLine();
StringTokenizer st = new StringTokenizer(buffer, ",");
Course[i]=st.nextToken();
Grade[i]=Integer.parseInt((st.nextToken()).trim());
}
input.close();
}
public static char menu(BufferedReader kb)throws Exception
{String buffer;
char input;
do
{
System.out.println("\nWhat would you like to do?");
System.out.println("E-Enter course data");
System.out.println("D-Display course data");
System.out.println("X-Exit program");
System.out.println("S-Sort list");
System.out.print("Enter E, D, X, S>>");
buffer=kb.readLine();
input=(buffer.toUpperCase()).charAt(0);
if(input=='E'||input=='D'||input=='X'||input=='S')
return input;
System.out.println("Invalid entry");
}while(true);
答案 0 :(得分:2)
'sort',[...]不能应用于(java.lang.String [],int [],java.io,BufferedReader)
这很合理,因为sort
有另一个签名:
int Grade[], String Course[], int quantity, BufferedReader kb)
如果您注意到,sort
期望int[]
成为其第一个参数,String[]
成为其第二个参数。所以,无论你在哪里调用它,都要改变你传递的参数的顺序。这可能是其他方法也显示此错误的原因。