I have a 2D array that displays a table of numbers

character_classes.jpg

I'm trying to populate a my 2D array using these coordinates.

numbers_for_array.png

My goal is to place place the number 1 at each of the coordinates. I'm trying to use a hasNext() method to read the txt file and then populate the array but im not having any luck. How do I read in each row and column of integers into the correct location.

Here is what I have so far:

import java.util.*;
import java.io.*;

public class CharacterClasses {

    static final int rows = 6;
    static final int columns = 5;

    public static void main(String[] args) throws Exception {

        int[][] surveyMatrix = new int[rows][columns];

        initMatrix(surveyMatrix);
        printArray(surveyMatrix);

    }


    // use this to populate survey matrix
    public static void initMatrix(int[][] M) throws Exception {



        File myFile = new File("survey2.txt");
        Scanner sc = new Scanner(myFile);

        while (sc.hasNextInt()) {

            for (int r = 0; r < rows; r++) {
                for (int c = 0; c < columns; c++) {
                    M[r][c] = sc.nextInt();
                }

            }
        }

    }

    public static void printArray(int [][] array){

        System.out.printf("%15s %23s\n", "Character Class", "Damage Range");
        System.out.printf("%41s\n", "1    2    3    4    5");

        String[] names = {"      Huntress:", "        Priest:", "      Summoner:",
                "   Necromancer:", "  Demon Knight:", "     Berserker:"};

        for (int row = 0; row < rows; row++) {

            System.out.printf("%5s ", names[row]);

            for (int col = 0; col < columns; col++) {
                System.out.printf("%5d", array[row][col]);
            }
            System.out.println();
        }

    }
}

You need something like (pseudo code)

while there is more input available
    read coordinates i and j
    M[i][j] = 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.