Question

I apologize if this question has been asked before. I searched for it and did not seem to be able to find a solution...

I am adding chars to a char[] from a .txt file.

I want to add all chars to the array except new-line chars ('\n'?).

This is what I am trying, which is not working.

import java.io.DataInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class StateReader extends FilterInputStream
{
    private static final int FIELD_SIZE = 25 * 25;
    static int readCount = 0;
    private DataInputStream ds; 

    public StateReader(InputStream in)
    {
       super(in);
       ds = new DataInputStream(in);
    }

    public char[] readState() throws IOException
    {
          char current;
          char[] stateChar = new char[FIELD_SIZE];
          int index = 0;

          while (index < stateChar.length) 
          {
             current = (char) ds.read();

             if(current == '\n')
             {
                current = (char) ds.read();    // Attempting to read char after
                stateChar[index++] = current;  // '\n' encountered, and store that.
             } else {
                stateChar[index++] = current;  // Stores whatever is read.
             }
          }

          for (int i = 0; i < stateChar.length; i++)
          {
             System.out.printf("" + stateChar[i]);  // Prints out contents of Array
                                                    // For troubleshooting...
          }

          return stateChar;
       }
    }
}

I'm sure there is a (better) way to do this.

As stated before, I'm trying to copy the contents of the text file to a char[], excluding new-line characters.

I am then basing a switch on the contents of this array and populating stuff accordingly. The problem is when the switch encounters a new-line character it doesn't know what to do. I tried adding a case for '\n' which recursively calls the same function... Needless to say, it didn't work. (This '\n' case was tried without the 'if' statement in the readState() method).

public State findState(int p, Cell c)
{
   State myState = null;
   switch (charStateList[p])
   {
      case 'x':   System.out.println("Read 'x':"+readCount);
                  readCount++;
                  myState = new Wall(c);
                  break;
      case 'o':   System.out.println("Read 'o':"+readCount);
                  readCount++;
                  myState = new Empty(c);
                  break;
      case '\n':  findState(p, c);
                  break;
      default:    System.out.println("Default:" + readCount);
                  readCount++;
                  myState = new Empty(c);
                  break;
   }

   return myState;
}

I tried .equals(), but won't work for a char.

Thanks for any help, it's much appreciated. And again, my apologize if this has been answered already.

Edit: Is there a way I can simply set a delimiter for the FileInputStream/DataInputStream, so that it is ignored?

Was it helpful?

Solution

I don't know how your file was structured. But here is a code that make adding chars to a char[] from a .txt file more easier:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TxtToChar {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String string = "";
        char[] c;
        try {
            Scanner scanner = new Scanner(new File("test.txt"));
            while (scanner.hasNext()){
                string += scanner.useDelimiter("\n").nextLine();

            }
             c = string.toCharArray();
             for(int i =0;i<c.length;i++){
                             if(c[i] !=' '){
                               System.out.println(c[i]);
                             }
             }
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

OTHER TIPS

Use scanner. The default delimiter is white-space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top