Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem in HashMap

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Problem in HashMap

    First of all, i got two class which is Owner class and OwnerFrame.

    Owner code:
    package virtuPet;
     
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;
     
    public class Owner {
     
    	private String name, userName;
    	private HashMap <String, String> owner = new HashMap <String, String>();
     
    	public void setOwnerName(String name)
    	{
    		this.name = name;
    	}
     
    	public String getOwnerName()
    	{
    		return name;
    	}
     
    	public void setUserName(String userName)
    	{
    		this.userName = userName;
    	}
     
    	public String getUserName()
    	{
    		return userName;
    	}
    	public void addOwner(String name, String userName)
    	{
    		owner.put ( name, userName);
    	}
     
    	public void saveOwner() throws IOException
    	{
    		File outFile = new File(userName+".txt"); // locating the data destination
    		FileWriter aFileWriter = new FileWriter(outFile,true);
    		PrintWriter aPrintWriter = new PrintWriter(aFileWriter);
     
    		StringBuilder outputSB = new StringBuilder();
     
    	    Set<Map.Entry<String, String>> setOwner = owner.entrySet();
    	   // Set<Map.Entry<Integer,Pet >> setPet = pets.entrySet();
     
    	    for (Map.Entry<String, String> me : setOwner) 
    	    {
    			outputSB.append (""+me.getKey()+ "; " + me.getValue()+"");
    	    }
     
     
    	}
     
    }

    OwnerFrame code:

    package virtuPet;
     
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class OwnerFrame extends JFrame
    {
    	private static final int WIDTH = 350;
    	private static final int HEIGHT = 150;
     
    	private JLabel nameLabel, userNameLabel;
    	private JTextField nameTextField, userNameTextField;
    	private JButton createOwnerB, cancelB;
     
    	private CreateOwnerButtonHandler cHandler;
    	private CancelButtonHandler cbHandler;
     
    	public OwnerFrame()
    	{
    		setTitle("Create New Owner");
    		setSize(WIDTH, HEIGHT);
     
    		Container pane = getContentPane();
    		GridLayout aGrid = new GridLayout(3,2);
    		pane.setLayout(aGrid);
     
    		nameLabel = new JLabel("Name");
    		userNameLabel = new JLabel("User Name");
     
    		nameTextField = new JTextField(25);
    		userNameTextField = new JTextField(15);
     
    		createOwnerB = new JButton("Create Owner");
    		cHandler = new CreateOwnerButtonHandler();
    		createOwnerB.addActionListener(cHandler);
     
    		cancelB = new JButton("Cancel");
    		cbHandler = new CancelButtonHandler();
    		cancelB.addActionListener(cbHandler);
     
    		pane.add(nameLabel);
    		pane.add(nameTextField);
    		pane.add(userNameLabel);
    		pane.add(userNameTextField);
    		pane.add(createOwnerB);
    		pane.add(cancelB);
     
    	}
     
    	private class CreateOwnerButtonHandler implements ActionListener 
    	{
    		public void actionPerformed(ActionEvent e) 
    		{
    			/*I am trying to solve how to put the OwnerName and Username value
                             *in the HashMap in the onwer class...i did trough method but the file is created but no data
                             *of the OwnerName and UserName in the text file*/
                            Owner a = new Owner();
    			a.setOwnerName(nameTextField.getText());
    			a.setUserName(userNameTextField.getText());
    			a.getOwnerName();
    			a.getUserName();
    			a.addOwner(a.getOwnerName(), a.getUserName());
     
    			try {
    				a.saveOwner();
    			} catch (IOException e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
    			setVisible(false);
    			JFrame aPetFrame = new PetFrame();
    			aPetFrame.setVisible(true);
     
    		}
    	}
     
    	private class CancelButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			System.exit(0);
    		}
    	}
     
    	public static void main(String[] args) 
    	{
    		JFrame aOwnerFrame = new OwnerFrame();
    		aOwnerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		aOwnerFrame.setVisible(true);
    	}
    }

    can someone help? i just need to solved on how to put input from textField to hashMap key and value.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Problem in HashMap

    For future reference, please include the details of the problem in your post rather than in your code.

    You never write anything to your file...the Map is appended to the StringBuffer, but you need to print that to your PrintWriter.

  3. The Following User Says Thank You to copeg For This Useful Post:

    Hikari9 (April 19th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem in HashMap

    ok sorry not really outline my problem..anyway thank for spotting my missing statment =D

    the next problem is how am i going to load the data in my text file to my hashmap? any suggestion?
    Last edited by Hikari9; April 19th, 2010 at 10:49 PM.

Similar Threads

  1. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 10:56 AM
  2. HashMap usage in Java
    By neo_2010 in forum Collections and Generics
    Replies: 2
    Last Post: September 18th, 2009, 02:12 AM