In meiner Hauptklasse wie gebe ich Argumente für die compare
Methode? Weil in Objekteingaben gefragt wird. Es zeigt mir ein Fehler:Der Methodenvergleich ist undefiniert für den Typ ASSIGNRES
The method compare(String, String) is undefined for the type ASSIGNRES;
package com.jspiders.Collection;
public class SStudent
{
int id;
String name;
int marks;
SStudent(int id,String name,int marks)
{
this.id=id;
this.name=name;
this.marks=marks;
}
}
package com.jspiders.Collection;
import java.util.Comparator;
public class Id implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1=(Student)o1;
Student s2=(Student)o2;
if(s1.id==s2.id)
return 0;
else if(s1.id>s2.id)
return 1;
else
return -1;
}
}
package com.jspiders.Collection;
import java.util.Comparator;
public class Name implements Comparator
{
public int compare(Object o3,Object o4)
{
Student s3=(Student)o3;
Student s4=(Student)o4;
return s3.name.compareTo(s4.name);
}
}
package com.jspiders.Collection;
public class Marks
{
public int compare(Object o5,Object o6)
{
Student s5=(Student)o5;
Student s6=(Student)o6;
if(s5.marks==s6.marks)
return 0;
else if(s5.marks>s6.marks)
return 1;
else
return -1;
}
}
package com.jspiders.Collection;
import java.util.Scanner;
public class ASSIGNRES
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
/*While(true)
{
System.out.println("create id");
System.out.println("create name");
System.out.println("create marks");
System.out.println("enter the choice");*/
int choice=sc.nextInt();
switch(choice)
{
case 1:System.out.println("enter the id no");
int idd=sc.nextInt();
int idd1=sc.nextInt();
Object ans=(int)idd;
Object ans1=(int)idd1;
compare(ans, ans1);
break;
case 2:System.out.println("enter the name");
String name1=sc.next();
String name2=sc.next();
compare(name1,name2);
break;
case 3:System.out.println("enter the marks");
int marks1=sc.nextInt();
int marks2=sc.nextInt();
compare(marks1,marks2);
break;
}
}
}
Der Fehler sagt alles: Es gibt keine Methode 'compare (String, String)' definiert in 'ASSIGNRES'. Wenn Sie versuchen, eines der oben definierten 'compare (Object, Object)' in 'ASSIGNRES' zu verschieben, erhalten Sie eine 'ClassCastException', da Sie' Integer' nicht in 'Student' umwandeln können. – Turing85
so für das zu überwinden, was ich tun muss? –
Was vergleichen Sie in Fall 2 und 3? Sollte es Objekte benutzen - und wenn ja, welchen Typ? – MaxZoom