Results 1 to 8 of 8
  1. #1
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Creating classes given a string

    I'm reading strings in from a file and need to create Place classes using these strings

    Java Code:
    String[] parts = CurrentLine.split("\\s+");
    												
    			if (parts[0].equals("place")) {				//Still need to check format of place name.
    		
    				Place parts[1]=m.newPlace(parts[1], Integer.parseInt(parts[2]), Integer.parseInt(parts[3]));
    			}
    parts[1] is obviously a string and this is where it fails, I'm not sure how to use it to make a Place by that name.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Creating classes given a string

    Sounds like you want to create Place objects, not classes. And what type is m? Place isn't a standard class. What is it from?
    Get in the habit of using standard Java naming conventions!

  3. #3
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: Creating classes given a string

    Sorry, I typed that up in a hurry.
    I am trying to create Place objects, m is a Map object that has the newPlace(string s, int x, int y ) method which creates a Place object and stores it in a hashset.
    Place and Map are classes that I have defined.
    Last edited by Crakrjack; 09-20-2013 at 05:46 AM.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Creating classes given a string

    I'm not sure if I'm answering your question, but I'll give it a shot.

    I'm guessing you want Place to have a string and a position (x, y)? So, make the Place class have those fields and accept them in a constructor. Then add them to a HashMap<String, Place>.

    Java Code:
    public class Place
    {
    	private final String name;
    	private final int x, y;
    	
    	public Place(String name, int x, int y)
    	{
    		this.name = name;
    		this.x = x;
    		this.y = y;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	
    	public int getX()
    	{
    		return x;
    	}
    	
    	public int getY()
    	{
    		return y;
    	}
    }
    Java Code:
    public static void main(String[] args)
    {
    	Map<String, Place> map = new HashMap<String, Place>();
    	map.put("Tokyo", new Place("Tokyo", 5, 6));
    }

  5. #5
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: Creating classes given a string

    Thanks a heap for that Andrew, I should be sorted now.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Creating classes given a string

    And exactly how does that differ from what I told you in #6 at --> Question regarding hash sets?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: Creating classes given a string

    Sorry Jim, it doesn't differ. It's just clicked for me now though. I've been a little slow on the uptake, too many late nights studying.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Creating classes given a string

    Quote Originally Posted by Crakrjack View Post
    Sorry Jim .. too many late nights studying.
    No problem! Been there (but didn't do as much of the latter as I should have) :)

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Creating New Classes
    By Syed Rizvi in forum New To Java
    Replies: 2
    Last Post: 07-31-2013, 04:47 AM
  2. Help with Creating Classes
    By d300539 in forum New To Java
    Replies: 5
    Last Post: 04-07-2013, 11:08 PM
  3. classes and creating objects
    By Latanyar in forum New To Java
    Replies: 4
    Last Post: 11-01-2010, 07:14 PM
  4. help creating two classes that function together
    By sinreaver in forum New To Java
    Replies: 6
    Last Post: 10-01-2008, 03:44 AM
  5. Creating inner classes
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •