Reputation: 117
I'm currently trying to use a Scanner to create a solver for a Sokoban-game, the problem I have right now is that my scanner is not working as intended, I usually have a text-file looking close to something like this:
## ###### #
##### #####
When I try to use my code, I want to scan every single character, including blankspaces, save them, create a 2D array, put them in there, this is my first steps. The problem is that my scanner is not woring as I hoped it would, I use the line:
sc.useDelimiter("s*");
This was my attempt to count to blankspaces, and it seems like it does, the problem is just that it switches row everytime it enters a blankspace. My code is right now looking like:
package soko;
import java.util.*;
import java.io.*;
public class Dowork {
File file;
Scanner sc;
char a;
static int lines;
Scanner lineScanner;
static int maxChar;
ArrayList array;
char[][] array2;
Scanner bc;
int n;
int m;
static int lines2;
public Dowork() throws Exception{
//lines += 1;
file = new File("C:/Users/joaki/Desktop/sokoban/readin.txt");
sc = new Scanner(file);
bc = new Scanner(file);
sc.reset();
bc.reset();
sc.useDelimiter("s*");
ArrayList<Character> array = new ArrayList<Character>();
while(sc.hasNext()) {
String line = sc.next();
if(maxChar <= line.length()) {
maxChar = line.length();
}
for (char ch:line.toCharArray()) {
array.add(ch);
}
if(sc.hasNextLine()) {
lines++;
}
}
sc.close();
while(bc.hasNextLine()) {
lines2++;
}
bc.close();
array2 = new char[lines][maxChar];
Iterator<Character> itemIterator = array.iterator();
while(itemIterator.hasNext()) {
itemIterator.next();
for (int q = 0; q < lines; q++) {
for(int r = 0; r < maxChar; r++) {
Character j = itemIterator.next();
array2[q][r] = j;
}
}
}
for (int q = 0; q < lines; q++) {
for(int r = 0; r < maxChar; r++) {
System.out.println(array2[q][r]);
}
}
}
public static void main(String[] args) throws Exception{
Dowork g = new Dowork();
System.out.println(lines2);
}
}
To give an example, when I use the txt-file with the following content:
########
# # .#
# $$.#
#### #
#@ ##
####
It counts the lines to 56, it obviously shouldnt be 56. Where did I do wrong?
Upvotes: 1
Views: 62
Reputation: 285405
One of the prime programming rules to remember: Keep it simple
On that note, why not simply use a plain vanilla Scanner, one whose delimiter has not been changed, and use it to get lines and only lines from the file. Then you can do with the lines as you need, such as convert to char array, or parse it with its own line Scanner if need be.
List<String> lines = new ArrayList<>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
array2 = new char[lines.size()][];
for (int i = 0; i < array2.length; i++) {
array2[i] = lines.get(i).toCharArray();
}
For example:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Scanner;
public class DoWork2 {
// assuming data in data.txt file in same path as class files
private static final String RESRC_PATH = "data.txt";
private char[][] array2;
public DoWork2(String resrcPath) {
InputStream is = getClass().getResourceAsStream(resrcPath);
Scanner sc = new Scanner(is);
List<String> lines = new ArrayList<>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
array2 = new char[lines.size()][];
for (int i = 0; i < array2.length; i++) {
array2[i] = lines.get(i).toCharArray();
}
if (sc != null) {
sc.close();
}
}
public String getLines2() {
StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
for (char[] cs : array2) {
formatter.format("%s%n", new String(cs));
}
formatter.close();
return sb.toString();
}
public static void main(String[] args) {
DoWork2 doWork2 = new DoWork2(RESRC_PATH);
System.out.println(doWork2.getLines2());
}
}
Upvotes: 2