user2541019
user2541019

Reputation:

Constructor of a 2d array only contains the last value

I am reading in a file using a scanner. The file is formatted with the first line being the dimensions of the array. The next line contains the 1st row, next line the second, etc. Example file:

3 3
1 2 3 
4 5 6
7 8 9

The problem I keep running into is that my array values seem to be all 9. The file is in ints but I need them in doubles. I also have a lot of print statements in there as I am trying to debug what was going on. My exception handling isn't finished. I will go back and beef that up after I can instantiate the array correctly. Any pointers would be appreciated. For example: a better way of getting the dimensions and instantiating the array with just opening the file once.

Updated but getting a nullPointerException

public class Help implements TopoMapInterface {

private String filename;
private File mapfile;
public double[][] baseMap;
public double[][] enMap;
public int enhancementLevel;

public Help(String filename) throws FileNotFoundException,
        InvalidFileFormatException {
    this.filename = filename;

    System.out.println("Reading in file: " + filename);

    String number = "";
    int row = 0;
    int col = 0;
    int count = 0;

    try {
        Scanner inputFile = new Scanner(new File(filename));

        while (inputFile.hasNextInt()) {
            row = Integer.parseInt(inputFile.next());
            col = Integer.parseInt(inputFile.next());
            System.out.println("Row : " + row);
            System.out.println("Col : " + col);
            baseMap = new double[row][col];
            System.out.println(baseMap[2][4]);
            for (int i = 0; i < baseMap.length; i++){
                for (int j = 0; j < baseMap[i].length; j++){
                    baseMap[i][j] = Double.parseDouble(inputFile.next());
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e.toString());
    }

Upvotes: 2

Views: 106

Answers (5)

AKS
AKS

Reputation: 19861

I am not sure why you are scanning the file two times, but if you are then here is the problem in your code

for (int r = 0; r < baseMap.length; r++){
    for (int c = 0; c < baseMap[r].length; c++){
        baseMap[r][c] = doubleVal;
    }
}

Following code will solve your problem:

try {
        Scanner inputFile = new Scanner(new File(filename));
        int count1 = 0;
        int r = -1, c = -1;
        baseMap = new double[row][col];
        while (inputFile.hasNextInt()) {
            count1++;
            number = inputFile.next();
            int intVal = Integer.parseInt(number);
            double doubleVal = (double) intVal;


            if (count1 > 2){
                if (count1 % row == 0)
                    r++;
                c = count1 % col;
                baseMap[r][c] = doubleVal;


            System.out.println(doubleVal+"*");
            }   
        }

        inputFile.close();

        System.out.println("Row = " + row + "  Col = " + col);
        for (r = 0; r < baseMap.length; r++) {
            for (c = 0; c < baseMap[r].length; c++) {
                System.out.print(baseMap[r][c] + " ");
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
    }

Also don't forget to put a break in your first scanning

if (count == 1) {
     row = Integer.parseInt(number);
} else if (count == 2) {
     col = Integer.parseInt(number);
     break;
}

Upvotes: 0

Archit Khosla
Archit Khosla

Reputation: 99

Assuming that your first line has the value of row first and then column

I would do this

int row = Double.parseDouble(inputFile.next());
int col = Double.parseDouble(inputFile.next());


for (int i = 0;i<row;i++){
for(j=0;j<col;j++)
{
baseMap[i][j]=Double.parseDouble(inputFile.next());
}}

This should store all your values in double as you want and I think so this is an easier way to store after reading from file.

I think so I got your question correct!

Upvotes: 2

No Idea For Name
No Idea For Name

Reputation: 11597

first of all when using scanner or any other stream, you should close them in the end using a finally statement

Scanner inputFile = null;

try{
  inputFile = new Scanner(new File(filename));
}
catch{}
finally{
   if(inputFile != null)
      inputFile.close();
}

this will ensure that you will release the scanner when done reading and not hold it to the next loop.

also, in your code, in the second while loop you seem to not close it right, and so in each loop this code is called:

    if (count1 > 2){
        for (int r = 0; r < baseMap.length; r++){
            for (int c = 0; c < baseMap[r].length; c++){

            baseMap[r][c] = doubleVal;
        }
    }  

last thing, you are opening the file twice! there is no need for that. at the very minimum you can change your code to:

public class Help implements TopoMapInterface {

private String filename;
private File mapfile;
public double[][] baseMap;
public double[][] enMap;
public int enhancementLevel;

public Help(String filename) throws FileNotFoundException,
        InvalidFileFormatException {
    this.filename = filename;

    System.out.println("Reading in file: " + filename);

    String number = "";
    int row = 0;
    int col = 0;
    int count = 0;
    Scanner inputFile = null;

    try {
        inputFile = new Scanner(new File(filename));
            number = inputFile.next();
            System.out.println(number);
            row = Integer.parseInt(number);
            number = inputFile.next();
            col = Integer.parseInt(number);
            int count1 = 0;
            baseMap = new double[row][col];
            while (inputFile.hasNextInt()) {
                count1++;
                number = inputFile.next();
                int intVal = Integer.parseInt(number);
                double doubleVal = (double) intVal;
            }// Closed your while loop here

            if (count1 > 2){
                for (int r = 0; r < baseMap.length; r++){
                    for (int c = 0; c < baseMap[r].length; c++){

                    baseMap[r][c] = doubleVal;
                }
            }   
            System.out.println(doubleVal+"*");
            }   

        }
        System.out.println("end of this while loop");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    finally
    {
        if(inputFile != null)
           inputFile.close();
    }

    try {
        System.out.println("Row = " + row + "  Col = " + col);
        for (int r = 0; r < baseMap.length; r++) {
            for (int c = 0; c < baseMap[r].length; c++) {
                System.out.print(baseMap[r][c] + " ");
            }
        }

    } catch (Exception e) {
        System.out.println(e.toString());
    }
}

Upvotes: 0

Every single time you read in a number when count1 > 2, you're then iterating over your entire matrix and inserting doubleVal into every single cell; the last value you see is 9, and so that's what you have everywhere.

More generally, if you're guaranteed to have correct input (i.e., you have a school exercise like this one), then you shouldn't be reading your dimension specifications inside a loop; you should read the first two ints, create the array based on that size, and then use a nested for loop to insert the values into the array.

Upvotes: 1

Leliel
Leliel

Reputation: 156

for (int r = 0; r < baseMap.length; r++){
    for (int c = 0; c < baseMap[r].length; c++){
        baseMap[r][c] = doubleVal;
    }
}

is where the problem lies. this iterates through every row and column of the array, setting every element to the current number since the last number you see is 9, that;s what gets left in the array.

what you really need is some other way of keeping track of row and column counts, rather than iterating through the array. perhaps a pair of counters?

int intVal = Integer.parseInt(number);
double doubleVal = (double) intVal;

should probably be replaced with double doubleVal = Double.parseDouble(number);

file reading improvements as given by No Idea For Name would significantly improve this code. though using is a java 7 and later only construct. for earlier versions. upgrade if you can, remember to close the resources otherwise.

Upvotes: 0

Related Questions