5 Replies - 8434 Views - Last Post: 24 April 2009 - 07:00 AM Rate Topic: -----

#1 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

adding items to a map

Posted 22 April 2009 - 08:38 PM

I have this code and I want to take user input and have it be entered and added to the map.
Both are string values.

I do not believe the map will support a add method. I think it is a put method of some sort.


Map<String, String> studentGrades  
			 = new HashMap<String, String>();
	   Scanner in = new Scanner(System.in);
	   studentGrades.put("Juliet", "89%"); 


Is This A Good Question/Topic? 0
  • +

Replies To: adding items to a map

#2 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: adding items to a map

Posted 22 April 2009 - 08:58 PM

Hey update. I got this

import java.awt.Color;
 import java.util.HashMap;
 import java.util.Map;
import java.util.Scanner;
 import java.util.Set;
import javax.swing.JOptionPane;
 
 /**
07:	This program demonstrates a map that maps names to colors.
08: */
 public class hashtest
 {
	public static void main(String[] args)
	{	  
		
	   Map<String, String> studentGrades  
			 = new HashMap<String, String>();
	   Scanner in = new Scanner(System.in);
	   studentGrades.put("Juliet", "89%");
	   studentGrades.put("Romeo", "78%");
	   studentGrades.put("Adam", "45%");
	   studentGrades.put("Eve", "100%");
 
	   String userName0 = JOptionPane.showInputDialog(
		 "Please enter a name:");
	   
	   String userName0grade = JOptionPane.showInputDialog(
		 "Please enter a grade:");
	   
	   studentGrades.put(userName0, userName0grade);
	   
	   System.out.println(studentGrades);
		
	   
	   
	   
	   
	   
	}
 }



Would somebody be able to help me make it so that it loops through the name and grade entry until the user enters the value "done"
Was This Post Helpful? 0
  • +
  • -

#3 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: adding items to a map

Posted 22 April 2009 - 09:11 PM

Hi!

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.JOptionPane;

/**
 * 07: This program demonstrates a map that maps names to colors. 08:
 */
public class Map2 {

	public static void main(String[] args) {

		Map<String, String> studentGrades = new HashMap<String, String>();
		String userName;
		String userGrade;
		String exit = "done";

		while(true) {
			userName = JOptionPane.showInputDialog("Please enter a name:");
			if(userName.equals(exit)) break;
			userGrade = JOptionPane.showInputDialog("Please enter a grade:");
			if(userGrade.equals(exit)) break;
			studentGrades.put(userName, userGrade);
		}

		System.out.println(studentGrades);
	}
}


:^:
Was This Post Helpful? 1
  • +
  • -

#4 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: adding items to a map

Posted 23 April 2009 - 07:33 PM

I ended up using something else but thanks for the help.

Here is what I ended up with just for people to see. I took a banking program I had in the past and used its menu and added options to make a simple student and grade program where users can add, remove, and edit students.

Here is my code


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.JOptionPane;


public class GradeDemo {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		

		Map<String, String> studentGrades = new HashMap<String, String>();
		String userName;
		String userGrade;
		String exit = "done";
		
		String choice = new String();

		//This while loop just keeps going back to the main menu until the user
		//chooses option 6, which is to exit
		 while (!(choice.equals("4"))){

			//Printing the main menu
			System.out.println("1. Add new Student and Grades");
			System.out.println("2. Change a grade");
			System.out.println("3. remove a student");
			System.out.println("4. Quit Program");
		   
		   System.out.println("Select an option, enter its number, and " +
					"press enter:");

			//Take choice input from user
			choice = in.next();

			//Make some space after the input to reduce clutter
			System.out.println("\n");

			//User chooses 1 to deposit
			if (choice.equals("1")){
				
				
			userName = JOptionPane.showInputDialog("Please enter a name: Enter 'done to quit");
			
			userGrade = JOptionPane.showInputDialog("Please enter a grade:");
		   
			studentGrades.put(userName, userGrade);
			System.out.println(studentGrades);
		}
			
			else  if (choice.equals("2")){
				
				
			userName = JOptionPane.showInputDialog("Enter the name of the student you would like to edit: ");
			
			
			if (studentGrades.containsKey(userName)==false){
				System.out.println("That student does not exist");
				
				
			}
			else {
			userGrade = JOptionPane.showInputDialog("Please enter their new grade:");
			studentGrades.put(userName, userGrade);
			System.out.println(studentGrades);
			}
			
			
		   
			
		}
			
			
			
			
			
				else  if (choice.equals("3")){
				
				
			userName = JOptionPane.showInputDialog("Enter the name of the student you would like to remove: ");
			
			
			if (studentGrades.containsKey(userName)==false){
				System.out.println("That student does not exist");
				
				
			}
			else {
			
			studentGrades.remove(userName);
			System.out.println(studentGrades);
			}
			
			
		   
			
		}
			
			
			
			
			
			
			
			
				else  if (choice.equals("4")){
				
					
			 System.out.println("The final students and grades are");		
			System.out.println(studentGrades);
			}

			//If they enter anything else, it is an invalid choice, start over
			else {
				System.out.println("Invalid option, please try again:");
			}
				
			
			
			
			
			
			
		   
			
		}
	   
			}
		
		
		
		 }
   


This post has been edited by Public Designs: 23 April 2009 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: adding items to a map

Posted 23 April 2009 - 07:46 PM

View PostPublic Designs, on 22 Apr, 2009 - 07:38 PM, said:

I have this code and I want to take user input and have it be entered and added to the map.
Both are string values.

I do not believe the map will support a add method. I think it is a put method of some sort.


Map<String, String> studentGrades  
			 = new HashMap<String, String>();
	   Scanner in = new Scanner(System.in);
	   studentGrades.put("Juliet", "89%"); 

This is what you seem to do, adding 2 String :Juliet" and "89%"
Was This Post Helpful? 0
  • +
  • -

#6 Public Designs  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 145
  • Joined: 08-November 08

Re: adding items to a map

Posted 24 April 2009 - 07:00 AM

I got what I needed. I was trying to take user input for student and their grades to make a simple gradebook thing. I figured it all out though and I posted my final code just for reference
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1