4 Replies - 874 Views - Last Post: 12 February 2012 - 02:21 PM Rate Topic: -----

#1 newbie915   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-January 12

Printing the key/value pairs of multiple maps

Posted 12 February 2012 - 01:25 PM

I'm having difficulty in the formatting of the key/value pairs from my maps. I need to display the first and last name from one map along with the appropriate id number and grade from another map. Right now, my output is formatted correctly, but prints the first and last name of each person twice. Any suggestions on what I'm doing wrong in my loop?

My class Student is as follows:

public class Student {
	
	private String firstName;
	private String lastName;
	private String grade;
	private String id;
	
	public Student( )
	{
		
	}
	
	
	void setFirstName(String n)
	{
		firstName = n;
	}
	String getFirstName()
	{
		return firstName;
	}
	
	void setLastName(String n)
	{
		lastName = n;
		
	}
	
	 String getLastName()
	{
		return lastName;
	}
	 
	 void setGrade(String g)
	 {
		 grade = g;
	 }
	 
	 String getGrade()
	 {
		return grade; 
	 }
	
	void setId(String id)
	{
		this.id = id;
	}
	
	 String getId()
	{
		return id;
	}

}



Any my main is as follows:

public class Gradebook {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		boolean stop = false;
		String grade = "";
		String fName = "";
		String lName = "";
		String id = "";
		Map<Object, String> students = new TreeMap<Object, String>();
		 Map<Object,String> idNum = new TreeMap<Object, String>();
		 Student s = new Student();
		
		 System.out.println("(A)dd, (R)emove, (M)odify, (P)rint, (Q)uit");
		 String choice = in.nextLine();
		 
		 while(stop == false)
		 {
			 if(choice.equalsIgnoreCase("A"))
			 {
				 System.out.print("Enter the student's first name: ");
				 fName = in.nextLine();
				 
				 System.out.print("Enter the student's last name: ");
				 lName = in.nextLine();
				 
				 System.out.print("Enter the student's grade: ");
				 grade = in.nextLine();
				 
				 System.out.print("Enter a unique integer id for student: ");
				 id = in.nextLine();
				 
				 s.setLastName(lName);
				 s.setFirstName(fName);
				 s.setGrade(grade);
				 s.setId(id);
				 students.put(s.getLastName(), fName);
				 idNum.put(s.getId(), grade);
				
				 System.out.println("(A)dd, (R)emove, (M)odify, (P)rint, (Q)uit");
				 choice = in.nextLine();
				 stop = false;
			 }
			 if(choice.equalsIgnoreCase("P"))
			 {
				 Set<Object> keySet = students.keySet();
			     Set<Object> keySet2 = idNum.keySet();
			   
			     for (Object key : keySet) 
				 {
					 String value = students.get(key);
					 
					 for(Object key2 : keySet2)
					 
					 {
						 String value2 = idNum.get(key2);
						 System.out.println(value + " " + key + " " + key2 + ": " + value2 );	
					 }	 
					 	
				 }
			     
			     System.out.println("(A)dd, (R)emove, (M)odify, (P)rint, (Q)uit");
				 choice = in.nextLine();
			 }
		 }
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Printing the key/value pairs of multiple maps

#2 illuss   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Printing the key/value pairs of multiple maps

Posted 12 February 2012 - 02:01 PM

the problem lies at your nested for loop. For each element in keySet, it will print every element in keySet2. So I have made some modification base on your code. I created a list of string that store every element in keySet and another list to store every element in keySet2, and finally use a for loop to display. lengthy, but works on your purpose:
Set<Object> keySet = students.keySet();
Set<Object> keySet2 = idNum.keySet();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
for (Object key : keySet) {
   String value = students.get(key);
   list1.add(value + " " + key);
}
for (Object key2 : keySet2) {
   String value2 = idNum.get(key2);
   list2.add(key2 + ": "+value2);
}
for (int i = 0; i < list1.size(); i++) {
    System.out.println(list1.get(i) + " " + list2.get(i)); 
    }


This post has been edited by illuss: 12 February 2012 - 02:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Printing the key/value pairs of multiple maps

Posted 12 February 2012 - 02:09 PM

you do not need a second 'for' loop. it would be better if you convert your keySet2 to an array using the method toArray(). Before start the 'for' loop
you should declare an int variable x and in the inner 'for' loop just get the key doing something like:
String value2 = idNum.get(arrayKeys[x])

So now you have the key which is arrayKeys[x] and the value. I would give you the whole code but it's not good idea you just need try yourself.
Was This Post Helpful? 1
  • +
  • -

#4 newbie915   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-January 12

Re: Printing the key/value pairs of multiple maps

Posted 12 February 2012 - 02:14 PM

View Postilluss, on 12 February 2012 - 02:01 PM, said:

the problem lies at your nested for loop. For each element in keySet, it will print every element in keySet2. So I have made some modification base on your code. I created a list of string that store every element in keySet and another list to store every element in keySet2, and finally use a for loop to display. lengthy, but works on your purpose:
Set<Object> keySet = students.keySet();
Set<Object> keySet2 = idNum.keySet();
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
for (Object key : keySet) {
   String value = students.get(key);
   list1.add(value + " " + key);
}
for (Object key2 : keySet2) {
   String value2 = idNum.get(key2);
   list2.add(key2 + ": "+value2);
}
for (int i = 0; i < list1.size(); i++) {
    System.out.println(list1.get(i) + " " + list2.get(i)); 
    }


Thank you for your help illuss, I knew my nested for loop was the issue, but had no idea how to get around it. I never even thought of using an array to hold the information because my mind was focused on just one solution. Very appreciated!
Was This Post Helpful? 0
  • +
  • -

#5 newbie915   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 12-January 12

Re: Printing the key/value pairs of multiple maps

Posted 12 February 2012 - 02:21 PM

View Postguido-granobles, on 12 February 2012 - 02:09 PM, said:

you do not need a second 'for' loop. it would be better if you convert your keySet2 to an array using the method toArray(). Before start the 'for' loop
you should declare an int variable x and in the inner 'for' loop just get the key doing something like:
String value2 = idNum.get(arrayKeys[x])

So now you have the key which is arrayKeys[x] and the value. I would give you the whole code but it's not good idea you just need try yourself.


Thanks Guido-Granobles, I got this part working using some advice from illuss, but using an array seems to be the method of choice. I will give your suggested method a try however, since it never hurts to know multiple ways to solve a problem. Thanks for your reply.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1