令牌对与Java匹配

时间:2018-01-21 18:59:27

标签: java logic

问题表明您需要匹配令牌(例如一对[]。例如:([)]不正确,([])正确,()(}不正确, """"是正确的,"""是不正确的。我认为逻辑错误可能出现在for循环中的if语句中。标记符号为:"(quotes),[,],{,},(,)

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
public class matchingtokens {
public static void main(String[] args) throws IOException {
    Scanner in=new Scanner(new File("File.txt"));
    while (in.hasNext()) {
    String input= in.nextLine();
    System.out.println("" + input);
    String[]a = input.trim().split("");
    ArrayList<Character>listOfQuotes =new ArrayList<Character>();
    ArrayList<Character>parenPart =new ArrayList<Character>();
    for (int i = 0; i < input.length();i++) {
        if (input.charAt(i) == '['|| input.charAt(i) == ']' || input.charAt(i) == '{' || input.charAt(i) == '}' || input.charAt(i) == '(' || input.charAt(i) == ')') {
            parenPart.add(input.charAt(i));
        } else if (input.charAt(i) == '\"') {
            listOfQuotes.add(input.charAt(i));
        } else {
        //  i++;
        }

    }
        System.out.println("quotes: " + listOfQuotes);
        System.out.println("others: " + parenPart);
    if (listOfQuotes.size() % 2 == 0) {
        if (parenPart.size() % 2 == 0) {
            if (parenPart.get(0) == ']' || parenPart.get(0) == '}' || parenPart.get(0) == ')') {
                System.out.println("INCORRECT:: CANNOT BEGIN WITH CLOSING");
                break;
            } else {
                for (int i = 0; i < parenPart.size() - 1; i++) {
                    if (parenPart.get(i) == '}' && parenPart.get(i-1) == '{') {
                        parenPart.remove(i);
                        parenPart.remove(i-1);
                        i = i - 2;

                    } else if (parenPart.get(i) == ']' && parenPart.get(i-1) == '[') {
                        parenPart.remove(i);
                        parenPart.remove(i-1);
                        i = i - 2;
                        continue;
                } else if (parenPart.get(i) == ')' && parenPart.get(i-1) == '(') {
                        parenPart.remove(i);
                        parenPart.remove(i-1);
                        i = i - 2;
                        continue;
                } else {
                        if(parenPart.size()== 0) {
                        System.out.println("CORRECT");
                    }else {
                            System.out.println("INCORRECT:: TOKENS DON'T MATCH UP");
                    }
                }

            }
        } }else {
            System.out.println("INCORRECT:: BRACKETS DON'T MATCH");
            break;
            }

    } else {
        System.out.println("INCORRECT:: \" DOES NOT MATCH \"");
        break;
    }

                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我不确定我明白你的意思但是试试

for (int i = 0; i < parenPart.size(); i++) {
//parenPart.size() not parenPart.size()-1
                        if (parenPart.get(i) == '}' && parenPart.get(i-1) == '{') {
                            parenPart.remove(i);
                            parenPart.remove(i-1);
                            i = i - 2;

                        } else if (parenPart.get(i) == ']' && parenPart.get(i-1) == '[') {
                            parenPart.remove(i);
                            parenPart.remove(i-1);
                            i = i - 2;
                            continue;
                        } else if (parenPart.get(i) == ')' && parenPart.get(i-1) == '(') {
                            parenPart.remove(i);
                            parenPart.remove(i-1);
                            i = i - 2;
                            continue;
                        } else {

                        }

                    }
                    //out of the for loop
                    if(parenPart.size()== 0) {
                        System.out.println("CORRECT");
                    }else {
                        System.out.println("INCORRECT:: TOKENS DON'T MATCH UP");
                    }