0

So i have a file that looks like this:

+-+-+-+ ("/n")
|S|   | ("/n")
+ + + + ("/n")
|   |E| ("/n")
+-+-+-+ ("/n")

/n being a new line in the file

and i want to have each character here as an entry in a 5x7 array. I do not know how to do it, this is what i have tried (along with a lot of other things. input is the file):

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

  Scanner input = new Scanner(new File("maze0.txt"));

  char maze[][] = new char[5][7];
  int charCount = 0;
     for (int row = 0; row < finalHeight; row++) {
        for (int col = 0; col < finalWidth; col++) {
           while (input.hasNextLine()){
               String line = input.nextLine();
              if ((row < finalHeight - 1 || col < finalWidth) && charCount < line.length()) {
                     maze[row][col] = line.charAt(charCount);
                     charCount += 1;
                     System.out.print(maze[row][col]);

But this prints out +S+ + which is not right. I am a brand new beginner programmer and having a hard time with this, thanks for any help you can offer.

I fixed it!! this is what I did:

  Scanner input = new Scanner(new File("maze0.txt"));




  char maze[][] = new char[5][7];
  input.nextLine();
     for (int row = 0; row < 5; row++) {
        String fileLine = input.nextLine();
        for (int col = 0; col < 7; col++) {
              char nextChar = fileLine.charAt(col);
              maze[row][col] = nextChar;
              System.out.print(maze[row][col]);
pootyy
  • 55
  • 6

6 Answers6

0

Convert the line to character array using .toCharArray() That will give you an array of all the characters. Then just feed them into your array.

it'd look something like..

// everytime we come to a new row, read a line from the file, convert it into letters then proceed on feeding them into columns.
for (int row = 0; row < finalHeight; row++) 
{
      String line = input.nextLine();
      Char[] chars = line.toCharArray();
      if(!input.hasNextLine())
            break;            // if there is no more lines to read, break the loop.
      for (int col = 0, i = 0; (col < finalWidth && i < chars.length); col++,i++) 
      {
        maze[row][col] = chars[i];
        System.out.print(maze[row][col]);
      }
}
gallickgunner
  • 472
  • 5
  • 20
0

Actually, while the screen might display +S+ +, you only have one value in your array - at maze[0][0] (a value of '+'). Your while loop reads the entire file before the for loops ever increment.For each line it reads, it sets maze[row][column] = line.charAt(charCount); -- but row and column never get incremented because, well, there's another line to read. So it reads another line and overwrites maze[0][0] to be the line.charAt(1) (because you incremented charCount). This character is the space. Then it loops back through because there's another line to read, and puts the 3rd character at maze[0][0]. So on and so forth. When it's read the entire file, then it steps through the for loops, but while (input.hasNextLine()) doesn't execute because it's already read the entire file.

  • oh ok that makes sense! I was having a hard time seeing what the loop was really doing with the while loop in there. Thank you! – pootyy Feb 05 '16 at 06:10
0

you just need two loops why are you running 3 loops?

Scanner sc = new Scanner(new File("maze.txt"));
String line = null;
for(int i = 0; i< 5;i++)
{
      line = sc.readLine()
      for(int j = 0; j < 7; j++)
      {
          maze[i][j] = line.charAt(j);
      }
}

This snippet should read the file and store it in a matrix.

Since you are reading the line in the inner loop, you are printing the diagonal.

adiSuper94
  • 23
  • 10
0

Here is a simple and efficient way to do it.

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("maze0.txt"));

    char maze[][] = new char[5][7];
    for (int i = 0; i < maze.length; i++) {
        //Get each line and convert to character array.
        maze[i] = input.nextLine().toCharArray();
    }
}
WillS
  • 362
  • 1
  • 12
0

You can read each line from the file and convert it to char array.

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(new File("maze0.txt")); 
    String b;
    char maze[][] = new char[5][7];
    for (int row = 0; row < 5; row++) {
        while ( scan.hasNextLine() ){
            b = scan.nextLine();
            maze[row] = b.toCharArray();
            System.out.println(maze[row]);
        }
    }    
    scan.close();
}
Divya
  • 53
  • 1
  • 7
0
import java.io.*;
import java.util.*;

public class B 
{
    public static void main(String...aaadf)throws FileNotFoundException
    {

        Scanner sc = new Scanner(new File("D://maze.txt"));
        char maze[][] = new char[5][7];
        String line = null;
        for(int i = 0; i< 5;i++)
        {
              line = sc.nextLine();
              for(int j = 0; j < 7; j++)
              {
                  maze[i][j] = line.charAt(j);
              }
        }
        for(int i = 0; i< 5;i++)
        {

              for(int j = 0; j < 7; j++)
              {
                  System.out.print(maze[i][j]);
              }
              System.out.println();
        }
    }
}
Deepak
  • 1
  • 1