0

I have file with this specific format:

0  
2 4   
0 1 A  
0 5 B  
1 1 A  
1 3 B  
2 6 A  
2 4 B  
3 6 A  
3 4 B  
4 6 A  
4 4 B  
5 1 A  
5 5 B  
6 6 A  
6 2 B  
  • line 1 = start state
  • line 2 = accept state
  • line 3 - n = transition table
  • 1st row = state in
  • 2nd row = state out
  • A,B = symbol

How can my FileReader in Java read these file into 5 different ArrayLists (start state, final state, state in, state out and symbol)?

3 Answers 3

3

It's best to use a Scanner here:

static class State {
    int in;
    int out;
    String symbol;

    State(int in, int out, String symbol) {
        this.in = in;
        this.out = out;
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return in + " " + out + " " + symbol;
    }
}

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

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

    int startState = Integer.parseInt(s.nextLine());
    List<Integer> acceptStates = new LinkedList<Integer>();
    List<State> states = new LinkedList<State>();

    Scanner st = new Scanner(s.nextLine());
    while (st.hasNextInt())
        acceptStates.add(st.nextInt());

    while (s.hasNextInt())
        states.add(new State(s.nextInt(), s.nextInt(), s.next()));

    System.out.println(startState);
    System.out.println(acceptStates);
    System.out.println(states);

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

3 Comments

I think that the OP wants the second line to become a list of accept states.
is it have the same code if i use 'in' and 'out' in string rather than int?
Almost, use next() instead of nextInt() then.
1

You can use the Scanner class to read the file (nextLine() is highly recommended). Since you know the positions of the items you need, you can then use the split method to parse the input string in what ever ArrayList you like.

5 Comments

Why not a simple BufferedReader?
can you explain a little bit more? i'm new in java
@thejh: I do not understand what do you mean exactly. @gin: You know that the first 2 lines of the file are the start state and the acceptance state. Also, you know that from lines 3 to n, you need to split the lines into rows. So basically, do a while loop with a counter, and depending on the value of the counter, you know on which line you are working, thus you can fill the arraylists accordingly. Please take a look at the links I have provided to get a better understanding of how you can implement such system.
hhmm,. so i must make a while loop read the line which is for each line the while loop send the content of these line to different arraylist?
Yes. Read the lines and split them if necessary. Since all of the data has a fixed position, you should be able to put the right data in the right arraylist.
0

Have a look at the StringTokenizer, use ArrayLists to build up your lists.

1 Comment

From its javadoc: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

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.