user1881401
user1881401

Reputation: 85

Java identifying tokens

I am working on an assignment where I have to look through a file and identify tokens. I am on the right track, I think. I have most of it working but I am having trouble identifying a particular character so I can choose to ignore it when identifying the token. Its a \n I have come to this conclusion because when I try to do tokens.get(6).equals('\n') in an if statement it comes up as false as if it isn't a \n I am having trouble identifying what is stored at this array index value. The text file looks like this

EDIT: I know its a \n because I did System.out.println("ddd" + tokens.get(6) +"dddd") and the output was ddd then a new line and dddd

EDIT 2: I added System.out.println(", unidenfified " + word); and this is the output enter image description here

read a
read b
c := a + b + 3
write c 

output supposed to be this

<read>, read
<id>, a
<read>, read
<id>, b
<id>, c
<assign>, :=
<id>, a
<add_op>, +
<id>, b
<add_op>, +
<number>, 3
<write>, write
<id>, c 

My output looks like this

<read>, read
<error>, unidenfified
<error>, unidenfified
<assign>, :=
<id>, a
<add_op>, +
<id>, b
<add_op>, +
<error>, unidenfified
<error>, unidenfified

I am not sure why this is happening. My code:

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.lang.Character;

public class Tokens {

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

        Scanner input = new Scanner(System.in); // Scanner for taking input from
                                                // the user

        String fileName;
        System.out.println("Enter the name of the file.");
        fileName = input.next();

        fileExists(fileName); // Checks to see if the file exists

        ArrayList<Character> arrayOfTokens = new ArrayList<Character>();
        ArrayList<String> assembled = new ArrayList<String>();
        readToArray(arrayOfTokens, fileName);
        assembled = assembleTokens(arrayOfTokens);

        for(int i = 0; i < assembled.size(); i++) {
             analyze(assembled.get(i));
         }

    }

    /*
     * readToArray goes through a file and adds all its elements in individual
     * character form. It is stored into an arraylist and it is then returned
     * 
     * @param storeChar: This is an arraylist of characters that the characters
     * will be saved into and then returned.
     * 
     * @param fileName: The filename that you want to take the data from.
     */
    private static ArrayList<Character> readToArray(
            ArrayList<Character> storeChar, String fileName) throws IOException {
        /*
         * Block of code to setup the fileInput stream to take in data from the
         * file. Reads character by character and stores into an arraylist. int
         * atChar: the current character the reader is at. Returns in int format
         * (Need to be converted to character later on) int currentIndex: to add
         * a character to an index. Increments until no more characters are left
         */
        FileInputStream fileInput = new FileInputStream(fileName);
        int atChar;
        int currentIndex = 0;

        /*
         * Loop to go through and add the converted character from an int to the
         * arraylist. Loops until atChar returns -1 which means no more
         * characters in file.
         */
        while ((atChar = fileInput.read()) != -1) {
            storeChar.add(currentIndex, (char) (atChar));
            currentIndex++;
        }
        fileInput.close();

        return storeChar;
    }

    /*
     * fileExists method makes sure the file the user enters exists in the
     * system. If it doesn't then the program will terminate before any further
     * code is executed.
     * 
     * @param fileName: Takes in a string paramater of the file name that you
     * want to if it exists.
     */
    private static void fileExists(String fileName) {

        boolean ifExists; // Boolean statement that will later be set to the
                            // value of whether the file exists or not

        File file = new File(fileName);
        ifExists = file.exists();

        if (ifExists == false) {
            System.out
                    .println("Unable to find the file. Will now close the program.");
            System.exit(0);
        }
    }

    private static ArrayList<String> assembleTokens(ArrayList<Character> tokens) {

        ArrayList<String> identified = new ArrayList<String>();
        int counter = 0;
        String concatinated = "";

        while (counter < tokens.size()) {
            if (!tokens.get(counter).equals(' ')) {
                concatinated += tokens.get(counter);
                counter++;
            } else {
                identified.add(concatinated);
                concatinated = "";
                counter++;
            }
        }

        return identified;
    }

    private static void analyze(String word) {
        if(word.equals("read")) {
            System.out.println("<read>, read");
        } else if(word.equals("write")) {
            System.out.println("<write>, write");
        } else if(word.equals(":=")) {
            System.out.println("<assign>, :=");
        } else if(word.equals("(")) {
            System.out.println("<lparen>, (");
        } else if(word.equals(")")) {
            System.out.println("<rparen>, )");
        } else if(word.equals("+") || word.equals("-")) {
            System.out.println("<add_op>, " + word);
        } else if(word.equals("*") || word.equals("/") || word.equals("//") || word.equals("%")) {
            System.out.println("<mult_op>, " + word);
        } else if(word.matches("[a-z]+[A-Za-z0-9]*")) {
            System.out.println("<id>, " + word);
        } else if(word.matches("\\d+(\\.\\d+)?")) {
            System.out.println("<number>, " + word);
        } else {
            System.out.println("<error>, unidenfified");
        }

    }
}

Upvotes: 0

Views: 1084

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44854

Simply filter out the \n when reading in the file

 if (atChar != '\n') {
     storeChar.add(currentIndex, (char) (atChar));
 }

Alternatively you could use

fileInput.readLine()

and then tokenize the String

Edit

Having a look after your last edit, if would be better to do a readLine and then using String.split(" "); to get your tokens

Upvotes: 1

Related Questions