By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

Using TreeMap

P: 3
Hi I posted s thread but did have any reply,
I am a new programmer and really wants someone to help me on how to use TreeMap for this code cos I want to it to be able to handle the options from 6 to 9 in the menu of options:

/*the next method to be implemented is the method presentResultsToUser() which is expected to print
a menu of options on the screen and also will ask the user to choose from that menu*/
public void presentResultsToUser(){ //method starst here

/* asking for a line of input from the user */
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//asks for a line of input
int i;
boolean yes = false; /*declaration of the boolean yes which is the first condition for the while
statement*/
while(!yes){ //while statements starts here
//asking the user to print lines of options
System.out.println("What would you like to see now? ");
System.out.println(" ");
System.out.println("1. Sorted list of all studentsí Last Names.");
System.out.println("2. Sorted list of all studentsí First Names");
System.out.println("3. Sorted list of all studentsí Matriculation Numbers.");
System.out.println("4. Sorted list of all studentsí Final Grades.");
System.out.println("5. Quit the program.");
System.out.println("6. List all students and scores sorted by Last Name.");
System.out.println("7. List all students and scores sorted by First Name.");
System.out.println("8. List all students and scores sorted by Matriculation Number.");
System.out.println("9. List all students and scores sorted by Final Grade.");
System.out.println("choose one: ");

String che = stdin.readLine(); //gets a line of input
int intInput = Integer.parseInt(che); //converts input to an integer type
if(intInput==5){//if starts
yes=true; /*the boolean is true if the input is 5 hence the while statement
will not be handled*/
}//if ends
else{ //else starts here
switch(intInput){ //switch statement starts here
case 1: //first case of the switch
Collections.sort(StdLstName); /*the collections framework is used to call the sort
method*/
System.out.print("Sorted list of Last Names: "); //prints the line
for(i=0; i<StdLstName.size();i++){ //for loop starts here.
String obj = (String)StdLstName.get(i); /*unveils the vector by type casting
that is removes the object component of the vector StdLstName and put it back
to the String*/
System.out.print(obj.toString()+", "); /*Prints out the string after the sorting
of the vector using collections framework*/
}//for ends here
System.out.println(" "); //prints out an empty line
break; //Prevents the execution from continuing up to case 2
case 2: //second case of the switch
Collections.sort(StdFstName); /*the collections framework is used to call the sort
method*/
System.out.print("Sorted list of First Names: "); //prints the line
for(i=0; i<StdFstName.size(); i++){//for loop starts here for the vector StdFstName
String obj = (String)StdFstName.get(i); /*unveils the vector by type casting
that is removes the object component of the vector StdFstName and put it back
to the String*/
System.out.print(" "+obj.toString()); /*Prints out the string after the sorting
of the vector using collections framework*/
} //for loop ends here
System.out.println(" "); //prints out an empty line
break; //Prevents the execution from continuing up to case 3
case 3: //third case of the switch
Collections.sort(StdMatNum); /*the collections framework is used to call the sort
method*/
System.out.print("Sorted list of Matriculation Numbers: "); //prints the line
for(i=0; i<StdMatNum.size();i++){ //for loop starts here.
Integer obj = (Integer)StdMatNum.get(i);/*unveils the vector by type casting
that is removes the object component of the vector StdMatNum and put it back
to the String as an integer type*/
System.out.print(" " +obj.toString());/*Prints out the string after the sorting
of the vector using collections framework*/
} //for loop ends here
System.out.print("\n"); //prints out an empty line
break; //Prevents the execution from continuing up to case4
case 4: //fourth case of the switch
Collections.sort(StdFinGrade); /*the collections framework is used to call the sort
method*/
System.out.print("Sorted list of Final Grades: "); //prints the line
for(i=0; i<StdFinGrade.size();i++){ //for loop starts here.
Double obj = (Double)StdFinGrade.get(i);/*unveils the vector by type casting
that is removes the object component of the vector StdFinGrade and put it back
to the String as a double type becasuse the final grade is a double type*/
System.out.print(" "+obj.toString());/*Prints out the string after the sorting
of the vector using collections framework*/
} // for loop ends here
System.out.print("\n"); //prints out an empty line
break; //Prevents the execution from continuing up to case5
case 5: //fifth case of the switch
break; //Prevents the execution from continuing up to the default
default: //default of the switch
System.out.println("Error! Input "+intInput+" is not a valid choice."); /*printing for
the default*/
} //switch ends
}//else ends
}//while ends
System.out.println("Thank you! Goodbye!");
Aug 3 '06 #1
Share this Question
Share on Google+
1 Reply


P: 1
For your information the TreeMap normally sorts it (by default) on the basis of the keys.So if you insert the keys as lastName and values as Scores then the TreeMap will automatically sort it by keys(i.e lastName).After that we just need to iterate and display the TreeMap.

You can use the following code for the Option 6 to 9.

Here scoreLstName is a TreeMap.
-------------------------------------------------------------

case 6: //sixth case of the switch
System.out.print("Sorted list of Last Names & Scores: "); //prints the line
Iterator iter = scoreLstName.keySet().iterator();
while (iter.hasNext()) {
Object objKey = iter.next();
Object objValue = scoreLstName.get(objKey);
// do whatever you need with the key and value
System.out.print(" "+objKey+ "-"+" "+objValue+","); /*Prints out the string after the sorting
}


of the vector using collections framework*/
//for loop ends here
System.out.println(" "); //prints out an empty line
break; //Prevents the execution from continuing up to case 7
-------------------------------------------------------------

Hope this suffice. :)
Aug 4 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.