Results 1 to 2 of 2
  1. #1
    joker760's Avatar
    joker760 is offline Member
    Join Date
    Dec 2011
    Location
    Northeast, US
    Posts
    3
    Rep Power
    0

    Default Java Question [Parsing ...2D ARRAY]

    So I'm in the process of creating a game which has a 2D ARRAY layout. A game where the user will be able to enter a command such as up,down,left, right and depending on where they take their 'player' they might win...lose or continue to the end. Kind of like the famous Zork Game. Anyways I'm trying to parse my map file and I can't seem to do it right. Here's my code

    Java Code:
    public GameMap(String f)  {
    		try {
    			Scanner s = new Scanner(new File(f));
    			
    			String [] dimensions = s.nextLine().split("\\s*x\\s");
    			int r = Integer.parseInt(dimensions[4]);
    			int c = Integer.parseInt(dimensions[3]);
    			
    			//initialize the map
    			map = new int [r][c];
    			
    			int row = 0;
    			while (s.hasNextLine()) {
    				String line = s.nextLine();
    				//if it isn't a comment
    				if (!line.matches("^#.*")){
    					String[] cols = line.split("\\s*,\\s*");
    					for (int i = 0; i < cols.length; i++)
    					map[row][i] = Integer.parseInt(cols[i]);
    					row++;
    					 
    						
    					}
    				}
    			
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    			
    		}
    My "map.txt" file is as follows
    Java Code:
    3 x 4
    # Coords Location	Action		Description
    0, 0	Home		Continue	This is the beginning point
    0, 1	Lumbridge	Continue	New players arrive at this location
    0, 2	Wilderness	Lose		You've been destroyed by the monsters
    1, 0	Varrock 	Continue	The capital of massive Kingdom Misthalin
    1, 1	Catherby	Continue	Small fishing town
    1, 2	Edgeville	Continue	Be careful, home of Wilderness Monsters
    2, 1	Falador		Lose		Game Over, you've been destroyed
    2, 2	Rimmington	Win			Congratulations, you just became King!
    Anyways If any of the lines in my .txt file is not a comment i need to split the line into the four different fields(coordinates, location, action, and description from map file) , store them in an array and then split the first element of this new array into the coordinates (say x,y) .
    However I don't know how to do this properly, can anyone help or at least point me in the right direction.
    I'm pretty new to Java.
    Thanks.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,800
    Rep Power
    25

    Default Re: Java Question [Parsing ...2D ARRAY]

    i need to split the line into the four different fields
    IF the fields are not delimited by some character, then you will need to use another technique to get to each of the fields.
    One way is to use the String class's substring method to get each of the four fields by their column position. Use with the trim method to strip off extra spaces.

Similar Threads

  1. Help With Array Parsing
    By Conan in forum New To Java
    Replies: 19
    Last Post: 01-24-2012, 02:12 AM
  2. parsing chars for an array
    By Ciaran.moore.clane in forum New To Java
    Replies: 5
    Last Post: 12-15-2011, 02:07 PM
  3. Parsing XML question
    By victor_sk in forum XML
    Replies: 2
    Last Post: 02-15-2011, 05:47 PM
  4. parsing XML in JAVA
    By krishkill in forum Advanced Java
    Replies: 5
    Last Post: 05-30-2010, 08:59 PM
  5. Parsing URL in Java
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:14 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
  •