简体   繁体   中英

Checking element in ArrayList between two double quotes

I'm writing a primitive version of programming language reader in Java for custom language that I made and I want to find out easiest way to print content of element from ArrayList that is located between two elements of double quotes. Here is source code:

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

public class PrimitiveCompiler {

    public static ArrayList<String> toks = new ArrayList<String>();

    public static void main(String[] args) throws FileNotFoundException {
        String content = readFile("C:\\program.txt");

        tokenize(content);
    }

    public static String readFile(String filePath) throws FileNotFoundException {
        File f = new File(filePath);
        Scanner input = new Scanner(f);

        StringBuilder b = new StringBuilder();

        while (input.hasNextLine()) {
            b.append(input.nextLine());
        }

        input.close();

        return b.toString();
    }

    public static ArrayList<String> tokenize(String fContent) {
        int i = 0;
        String tok = "";

        String contents = fContent.replaceAll(" ", "").replaceAll("\n", "").replaceAll("\t", "");

        for(int a = 0; a <= contents.length() - 1; a++) {
            tok += contents.charAt(a);
            i = a;

            if(tokenFinderEquals(tok, "WRITE")) {
                toks.add("WRITE");
                tok = "";
            }
        }

        System.out.println(toks);

        return null;

        }

    public static boolean tokenFinderEquals(String s1, String s2) {
        if(s1.equalsIgnoreCase(s2)) {
            return true;
        }

        return false;
    }
}

Content of text file right now is just WRITE and it succesfully finds it and add it to ArrayList . What I want to do is to count double quotes and when two double quotes are found in ArrayList to print out every element between them. Is it posibble or there's another, easier way to do this? Thanks in advance!

You'll need some kind of state to keep track of whether or not you're inside of a quote. For example:

boolean inQuote = false;
for (int a = 0; a <= contents.length() - 1; a++) {
  char c = contents.charAt(a);
  if (c == '"') {
    // Found a quote character. Are we at the beginning or the end?
    if (!inQuote) {
      // Start of a quoted string.
      inQuote = true;
    } else {
      // End of a quoted string.
      inQuote = false;
      toks.add(tok);
      tok = "";
    }
    // Either way, we don't add the quote char to `tok`.
  } else {
    tok += c;
    if (!inQuote && tokenFinderEquals(tok, "WRITE") {
      // Only look for "WRITE" when outside of a quoted string.
      toks.add(tok);
      tok = "";
    }
  }
}

Using a simple loop like this can start to get tough as you add more cases, though. You may want to look into writing a recursive descent parser .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM