0

I am trying to load a text file that contains characters and two numbers, something that looks like this

5 7
++++M++
+  +  +
++   ++
+  ++ +
+X+++++

where the 5 and 7 correspond to number of rows and columns. I am trying to read this line by line and then translate the numbers into values in an array, however for some reason I fail to even

     Scanner inputFile = new Scanner(new FileReader(fid));
maze_r = inputFile.nextInt();
maze_c = inputFile.nextInt();
int counter = 0;
   while(inputFile.hasNextLine()){

    rowArray = inputFile.nextLine();
  System.out.print("\n"+rowArray);


     char index;
       for(int ind = 0; ind == (maze_c-1);ind++){

            index = rowArray.charAt(ind);
            System.out.print(index);
            if(index == ' ')
                theMaze[counter][ind] = 0;
            if(index == '+')
                theMaze[counter][ind] = 1;
            if(index == 'M')
                theMaze[counter][ind] = 2;
             if(index == 'X')
                theMaze[counter][ind] = 3;

  }
   }
   System.out.print(Arrays.toString(theMaze));
   }

When I do this it prints out the rows as characters but will print a void for the array I have tried to make. I do not know what I am doing wrong, for some reason it will not compute the for loop in the while loop. When I wrote like a system.out.print inside the loop nothing printed. What am I doing wrong?

3
  • 1
    counter is not getting incremented. Commented Nov 7, 2014 at 7:49
  • 2
    You should really put a little more effort in your indentations. It might actually help you noticing your errors. Commented Nov 7, 2014 at 7:49
  • On short algorithm don't hesitate to test it by hand, noting the values and going through the statements. It'll then appear clearly whats going wrong. Commented Nov 7, 2014 at 7:51

2 Answers 2

1

The second condition in for loop is to be true for the loop to continue:

ind == (maze_c-1) // ⇒ your loop breaks because ind is not equal to the rvalue

It should be changed to:

ind < maze_c

Treat it as “while” condition.

Sign up to request clarification or add additional context in comments.

Comments

0

for(int ind = 0; ind == (maze_c-1);ind++){ your loop condition is wrong, ind==(maze_c-1) is always true,so your code in loop,can't be run,then the result of System.out.print(Arrays.toString(theMaze)) is null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.