Question

I am trying to load a 2d int array that I loaded into a file in a previous method of my code. When i reach the method LoadFromFile, I can't seem to get my head around how I should implement this back to my Course class and add it back to the Map that was declared in the class. This is the code i have at the moment with the instructions my professor has given us and my attempt to try and figure this out. At this point i was pretty much doing nonsense stuff hoping it would trigger something to get this to work.

thanks in advance for any help or guidance!

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.awt.Point;
import java.awt.Dimension;

public class TileMap implements Loadable
{
private Dimension Size;
private int[][] Map;

public TileMap(int NewWidth, int NewHeight)
{
    Size = new Dimension(NewWidth, NewHeight);
    Map = new int[NewWidth][NewHeight];
}

// accessors and mutators for Size
public Dimension GetSize()
{
    return Size;
}
public void SetSize(int NewWidth, int NewHeight)
{
    Size = new Dimension(NewWidth, NewHeight);
    Map = new int[NewWidth][NewHeight];
}

// accessor and mutator for Map
public int[][] GetMap()
{
    return Map;
}
public void SetMap(int[][] NewMap)
{
    // copy the reference
    Map = NewMap;
}

/**
 * Loads a TileMap from a file. The first line of the file will have the width, the
 * first dimension of the array. The second line will have the height, the second
 * dimension of the array. Then the values in the array should follow as demonstrated
 * below.
 * 
 * Example:
 * 6\n
 * 6\n
 * 2,1,1,0,0,2\n
 * 0,0,1,0,0,1\n
 * 0,0,1,0,0,1\n
 * 0,2,1,0,0,1\n
 * 0,0,1,1,1,1\n
 * 0,0,1,0,0,1\n
 * 
 * @param Filename the file to load from
 * @return the empty string "" if the load succeeds, or the exception message if it fails
 */

public String LoadFromFile(String Filename)
{
    try
    {
        File nf = new File(Filename);
        Scanner in = new Scanner(nf);
        String text = "";
        int x =0;
        //load file and assign all scans
        //to respected properties within class
        while(in.hasNextLine())
        {
            text = in.nextLine() + "\n";

        }

        int w = Integer.parseInt(text[0]);
        int h = Integer.parseInt(text[1]);
        SetSize(w,h);
        int[][] newMap = null;
        int k = 2;
        for(int i = 0; i < GetSize().width;i++)
        {
            for(int j = 0; j < GetSize().height; j++)
            {
                newMap[i][j] = Integer.parseInt(text[k]);
                k++;
            }
        }

        SetMap(newMap);

        in.close();
    }
    catch(FileNotFoundException e)
    {

        return Filename + " (No such file or directory)";
    }
    catch(Exception f)
    {
        f.getStackTrace();
    }

    return "";  
}
Was it helpful?

Solution

I see you're in CS 49J. Here's how we did it,

public String LoadFromFile(String Filename)
{
    // YOUR CODE HERE
    try
    {
        File inFile = new File(Filename);
        Scanner inText = new Scanner(inFile);
        inText.useDelimiter("[\\s,\r\n]+");
        int tempW = inText.nextInt();
        int tempH = inText.nextInt();

        SetSize(tempW, tempH);

        for (int i = 0; i < tempW; i++)
        {
            for (int j = 0; j < tempH; j++)
            {
                if (inText.hasNextInt())
                {
                    int temp = inText.nextInt();
                    Map[i][j] = temp;
                }
            }
        }

        inText.close();
        return "";
    }
    catch (Exception e)
    {
        String dir = " (No such file or directory)";
        return Filename + dir;
    }
}

Make sure to check your Map.tm between method calls so you have the right information in there.

OTHER TIPS

It's fairly easy and you started in the right direction.

You read the two lines assign them to width and height which you've done.

What you did wrong was the following, you shouldn't read the whole file at once, read the first two lines parse them into numbers. You will know how much more lines you will have to read (it's w)

If you know how many lines there will be you have to do the following w times: read the next line parse the next line (split it on the comas) into a string array (java will do this with string.split) iterate through the string array, convert each of the variables into numbers then put them into the corresponding array elements

also your newMap is null, you should initialize it to new int[w][h]

If values in the file are look like in your comment:

 * Example:
 * 6\n
 * 6\n
 * 2,1,1,0,0,2\n
 * 0,0,1,0,0,1\n
 * 0,0,1,0,0,1\n
 * 0,2,1,0,0,1\n
 * 0,0,1,1,1,1\n
 * 0,0,1,0,0,1\n

You should use the text variable as an array of strings.. The way you did resulted that, the text variable only contained the last line of your file.

String[] text = new String[];
int x = 0;

while(in.hasNextLine())
        {
            text[x++] = in.nextLine();
        }

        int w = Integer.parseInt(text[0]);
        int h = Integer.parseInt(text[1]);

Then you also have problems with the lines above 2.. Each lines contains numbers separated by commas, so you have to split them by comma delimiter, then you can assign each value to the right dimensions of the newMap:

for (int i = 2; i <= text.length; i++) {
   String[] temp = text[i].split(",");
   for (int j = 0; j <= temp.length-1, j++) { //temp.length-1 is because the last string will be a number + "\n" 
      newMap[i-2][j] = Integer.parseInt(temp[j]);
   }
   newMap[i-2][temp.length] = Integer.parseInt(temp[temp.length].substring(0,1)); //taking care of the last string of the row
}

And you shouldn't point the newMap to null, instead use

int[][] newMap = new int[w][h];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top