473,769 Members | 7,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using TreeMap

3 New Member
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 presentResultsT oUser() 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 presentResultsT oUser(){ //method starst here

/* asking for a line of input from the user */
BufferedReader stdin = new BufferedReader( new InputStreamRead er(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.prin tln("What would you like to see now? ");
System.out.prin tln(" ");
System.out.prin tln("1. Sorted list of all students’ Last Names.");
System.out.prin tln("2. Sorted list of all students’ First Names");
System.out.prin tln("3. Sorted list of all students’ Matriculation Numbers.");
System.out.prin tln("4. Sorted list of all students’ Final Grades.");
System.out.prin tln("5. Quit the program.");
System.out.prin tln("6. List all students and scores sorted by Last Name.");
System.out.prin tln("7. List all students and scores sorted by First Name.");
System.out.prin tln("8. List all students and scores sorted by Matriculation Number.");
System.out.prin tln("9. List all students and scores sorted by Final Grade.");
System.out.prin tln("choose one: ");

String che = stdin.readLine( ); //gets a line of input
int intInput = Integer.parseIn t(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.sor t(StdLstName); /*the collections framework is used to call the sort
method*/
System.out.prin t("Sorted list of Last Names: "); //prints the line
for(i=0; i<StdLstName.si ze();i++){ //for loop starts here.
String obj = (String)StdLstN ame.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.prin t(obj.toString( )+", "); /*Prints out the string after the sorting
of the vector using collections framework*/
}//for ends here
System.out.prin tln(" "); //prints out an empty line
break; //Prevents the execution from continuing up to case 2
case 2: //second case of the switch
Collections.sor t(StdFstName); /*the collections framework is used to call the sort
method*/
System.out.prin t("Sorted list of First Names: "); //prints the line
for(i=0; i<StdFstName.si ze(); i++){//for loop starts here for the vector StdFstName
String obj = (String)StdFstN ame.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.prin t(" "+obj.toString( )); /*Prints out the string after the sorting
of the vector using collections framework*/
} //for loop ends here
System.out.prin tln(" "); //prints out an empty line
break; //Prevents the execution from continuing up to case 3
case 3: //third case of the switch
Collections.sor t(StdMatNum); /*the collections framework is used to call the sort
method*/
System.out.prin t("Sorted list of Matriculation Numbers: "); //prints the line
for(i=0; i<StdMatNum.siz e();i++){ //for loop starts here.
Integer obj = (Integer)StdMat Num.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.prin t(" " +obj.toString() );/*Prints out the string after the sorting
of the vector using collections framework*/
} //for loop ends here
System.out.prin t("\n"); //prints out an empty line
break; //Prevents the execution from continuing up to case4
case 4: //fourth case of the switch
Collections.sor t(StdFinGrade); /*the collections framework is used to call the sort
method*/
System.out.prin t("Sorted list of Final Grades: "); //prints the line
for(i=0; i<StdFinGrade.s ize();i++){ //for loop starts here.
Double obj = (Double)StdFinG rade.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.prin t(" "+obj.toString( ));/*Prints out the string after the sorting
of the vector using collections framework*/
} // for loop ends here
System.out.prin t("\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.prin tln("Error! Input "+intInput+ " is not a valid choice."); /*printing for
the default*/
} //switch ends
}//else ends
}//while ends
System.out.prin tln("Thank you! Goodbye!");
Aug 3 '06 #1
1 7742
rajamuna
1 New Member
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.prin t("Sorted list of Last Names & Scores: "); //prints the line
Iterator iter = scoreLstName.ke ySet().iterator ();
while (iter.hasNext() ) {
Object objKey = iter.next();
Object objValue = scoreLstName.ge t(objKey);
// do whatever you need with the key and value
System.out.prin t(" "+objKey+ "-"+" "+objValue+",") ; /*Prints out the string after the sorting
}


of the vector using collections framework*/
//for loop ends here
System.out.prin tln(" "); //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
6825
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
1
8495
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
5941
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 X509Certificate ( *.cer file ) so that it can be used to email as attachment. The real part is Encrypting using X509Certificate and CryptoServiceProvider.
1
567
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 want to use a callback from the client side to update webcontrols based on user input without using postback. I am seeking a way to stop the compile errors. using System; using System.Data;
10
2662
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 | don't, I need to know. Flames welcome.
17
3516
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 using std::cin; using std::cout;
8
4916
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 namespace std;' unless the std namespace was declared first. This triggered my curiosity, and I tried to find out what the ANSI C++ standard had to say about this. I'm unable to find a conclusion, and hope someone here have a clue to spare. ...
0
2063
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 lastnname and matriculation after sorting them by last name. I did the sorting already using the collections framework, now my problem is how to use the treemap so that i would get the list.
3
8297
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
3
3005
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; private int membernumber; private Map<String, Map<Integer, Holding>> stock;
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.