Thursday 15 September 2011

java - Single Scanner statment askes for repeated user input for sorting collections using comparator interface -



java - Single Scanner statment askes for repeated user input for sorting collections using comparator interface -

in below code scanner statement request user input multiple times before can sort.

if there are n elements sort asks user input n-1 times , have tried debug , couldn't fins proper reason.

package com.sort.comparableandcomparator; import java.util.arraylist; import java.util.collections; import java.util.list; import java.util.scanner; public class pupil implements comparable<student> { private int id,score; private string firstname,lastname; scanner scan=new scanner(system.in); public student(int id,int score,string firstname ,string lastname){ this.id=id; this.score=score; this.firstname=firstname; this.lastname=lastname; } //getters , setters public int getid() {return id;} public void setid(int id) {this.id = id;} public int getscore() {return score;} public void setscore(int score) {this.score = score;} public string getfirstname() {return firstname;} public void setfirstname(string firstname) {this.firstname = firstname;} public string getlastname() {return lastname;} public void setlastname(string lastname) {this.lastname = lastname;} @override public int compareto(student studentobj) { //user input system.out.println("1:sortby id 2:sortby score 3:sortby firstname 4:sortby lastname"); int input=scan.nextint(); //switch statement switch(input){ case 1:system.out.println("comapring ids"); if(this.getid()<studentobj.getid()){ system.out.println("comparing "+this.getid()+" "+studentobj.getid()); homecoming -1; }else if(this.getid()>studentobj.getid()){ system.out.println("comparing "+this.getid()+" "+studentobj.getid()); homecoming 1; }else homecoming 0; case 2:system.out.println("comapring scores"); if(this.getscore()<studentobj.getscore()){ homecoming -1; }else if(this.getscore()>studentobj.getscore()){ homecoming 1; }else homecoming 0; case 3:system.out.println("comapring firstname"); if(this.getfirstname().compareto(studentobj.getfirstname())<0){ homecoming -1; }else if(this.getfirstname().compareto(studentobj.getfirstname())>0){ homecoming 1; }else homecoming 0; case 4:system.out.println("comapring lastname"); if(this.getlastname().compareto(studentobj.getlastname())<0){ homecoming -1; }else if(this.getlastname().compareto(studentobj.getlastname())>0){ homecoming 1; }else homecoming 0; default:system.out.println("no right input selected!!!"); homecoming 0; } } @override public string tostring() { homecoming "student [id=" + id + ", score=" + score + ", firstname=" + firstname + ", lastname=" + lastname +"]"+"---"; } public static void main(string[] args) { list<student> studentlist=new arraylist<student>(); studentlist.add(new student(1,42,"john","b")); studentlist.add(new student(2,52,"bob","e")); studentlist.add(new student(5,44,"christian","g")); studentlist.add(new student(7,44,"jerry","j")); system.out.println("sortinng!!!"); collections.sort(studentlist); system.out.println("sorting completed!!"); system.out.println(studentlist+"\n"); } }

java collections java.util.scanner comparator

No comments:

Post a Comment