实现对学生的成绩按总分排序,程序代码如下:本人感谢java不久,望各位前辈多多指点,谢谢
/*Exception in thread "main"
java.lang.NumberFormatException: For input string: ""
at java.lang.
NumberFormatException.forInputString(NumberFormatException.
java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at Comparable03.main(Comparable03.java:10)
请按任意键继续. . .*/
//实现对学生成绩排名
import java.util.*;
import java.lang.String;
public class Comparable03{
static String str="|张飞:60,90,87,76,87|关羽:60,90,87,76,87|刘备:76,90,76,76,87|曹操:60,76,87,76,87|诸葛亮:60,90,87,76,87|孙权:60,76,77,76,87|黄盖:87,90,87,76,87|庞涓:60,87,87,76,87|赵子龙:60,90,87,76,87|曹植:60,76,87,76,87|曹培:60,90,87,76,87|周瑜:60,90,87,76,87";
public static void main(String[] args){
String sArr[]=str.split("\\|");
Student[] st=new Student[sArr.length];
for(int i=0;i<sArr.length;i++){
st[i]=new Student(sArr[i].split("\\:")[0],Integer.parseInt(sArr[i].split("\\,")[0]),Integer.parseInt(sArr[i].split("\\,")[1]),Integer.parseInt(sArr[i].split("\\,")[2]),Integer.parseInt(sArr[i].split("\\,")[3]),Integer.parseInt(sArr[i].split("\\,")[4]));
}
Arrays.sort(st);
System.out.println("姓名 语文 数学 英语 化学 生物 总分");
for(Student s:st){
System.out.println(s);
}
}
}
class Student implements Comparable<Student>{
private String name;
private int chain;
private int math;
private int yy;
private int hx;
private int sw;
private int zf;
public Student(String name,int chain,int math,int yy,int hx,int sw){
this.name=name;
this.chain=chain;
this.math=math;
this.yy=yy;
this.hx=hx;
this.sw=sw;
this.zf=this.chain+this.math+this.yy+this.hx+this.sw;
}
public int compareTo(Student s){
int cha=this.zf-s.zf;
if(cha!=0){
return cha;
}else{
return this.name.compareTo(s.name);
}
}
public String toString(){
String s=name+" "+chain+" "+math+" "+yy+" "+hx+" "+sw+" "+zf+" ";
return s;
}
}//为什么老是报上面的异常啊我调试了好长时间
------解决方案--------------------你的数组分隔后出现了空字符
import java.util.Arrays;
public class ttt{
static String str="张飞:60,90,87,76,87
------解决方案--------------------关羽:60,90,87,76,87
------解决方案--------------------刘备:76,90,76,76,87
------解决方案--------------------曹操:60,76,87,76,87
------解决方案--------------------诸葛亮:60,90,87,76,87
------解决方案--------------------孙权:60,76,77,76,87
------解决方案--------------------黄盖:87,90,87,76,87
------解决方案--------------------庞涓:60,87,87,76,87
------解决方案--------------------赵子龙:60,90,87,76,87
------解决方案--------------------曹植:60,76,87,76,87
------解决方案--------------------曹培:60,90,87,76,87
------解决方案--------------------周瑜:60,90,87,76,87";
public static void main(String[] args){
String sArr[]=str.split("\\
------解决方案--------------------");
String[] st=new String[sArr.length];
for(int i=0;i<sArr.length;i++){
String nameString=sArr[i].split(":")[0];
String coreString=sArr[i].split(":")[1];
Student st1=new Student(sArr[i].split(":")[0],Integer.parseInt(coreString.split(",")[0]),Integer.parseInt(coreString.split(",")[1]),Integer.parseInt(coreString.split(",")[2]),Integer.parseInt(coreString.split(",")[3]),Integer.parseInt(coreString.split(",")[4]));
System.out.println("姓名 语文 数学 英语 化学 生物 总分");