473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using TreeMap

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
1 7723
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

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
1
by: Mark Wayne | last post by:
Is is possible to use TreeMap in the javascript <SCRIPT> portion of HTML? If so, how? Thanks, mark
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
0
by: Thiero | last post by:
I am a studend in programming and I find it difficult to use treemap, can any one help me on how to use treemap. Specially the purpose is to use treemap and give a list of students firstname...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
3
by: millz | last post by:
I am trying to create a tree map within a TreeMap. Any help would be great. goes a bit like this: public class ChessImpl implements CHESS { private Map<String, Integer> name; ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.