如何在java中查找文本文件中的副本

ukxgm1gy  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(296)

我正在用java设计一个编译器。在这种情况下,我从文本文件中读取输入,并将该输入有条件地存储到符号表中。现在我想检查重复数据,然后不想将重复数据添加到表中。
我使用了set,我知道它不允许重复,程序没有将重复的数据添加到表中,但它也在打印插入的数据,我不允许。我想当文本中存在重复数据时,程序应该说数据已经存在。
下面是将数据添加到表中的代码。

case 10:
                    if(str.contains("=")){
                        System.out.println("Token name = otop");
                        System.out.println("Attribute value = ASN");

                        if(str.startsWith("=")){
                            break;
                        }
                        else{
                        try{
                        String a[] = str.split(" ");
                        type = a[0];
                        lexeme = a[1];
                        value = a[3];
                        if(value.endsWith(";")){
                        value = value.replace(";", "");
                        if(type.contains("int") && value.matches("[-+][0-9]*")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("float") && value.matches("[0-9].[0-9]*")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("string") && value.matches("\"[A-Z a-z 0-9]*\"")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("char") && value.matches("\'[A-Za-z0-9]\'")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else{
                            System.out.println("ERROR: Type MissMatch!");
                        }
                        }
                        else if(!value.endsWith(";")){
                            System.out.println("\nERROR: ; expected on line number " +lineNumber.getLineNumber() + " in the text file");
                        }
                        }
                        catch(Exception e){
                            System.out.println("Error: Try to declare variale with spaces e.g int a = 5;");
                        }
                        }
                        break;
                    }

我得到的结果是:

f
l
o
a
t

a

=

5
.
5
;
Token name = otop
Attribute value = ASN
Data Inserted in the table!
f
l
o
a
t

a

=

5
.
5
;
Token name = otop
Attribute value = ASN
Data Inserted in the table!
----------------------------------------------------------------------------------

            --Symbol Table--
Lexeme      Token name   Type       Attribute value
a           id           float      5.5
----------------------------------------------------------------------------------

我怎样才能解决这个问题?作为参考,我附上以下全部代码:

import java.io.FileReader;
import java.util.*;
import java.io.LineNumberReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class main {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        LineNumberReader lineNumber;

        FileReader newFile = new FileReader( "C:/temp/sourcecode.txt");
        lineNumber = new LineNumberReader(new FileReader("C:/temp/sourcecode.txt"));

        Scanner scanFile = new Scanner(newFile);
        //Scanner scan = new Scanner(System.in);
        //char s;
        String str, lexeme, type, value;

        //String str1 = "\"[A-Za-z]*\"";
        Set<String> tabledata = new HashSet<>();

        while(scanFile.hasNextLine() && lineNumber.readLine() != null){
        str = scanFile.nextLine();
        //lexeme.add(str);
        if(!str.startsWith("//") && !str.startsWith("/*") && !str.endsWith("*/")) {

        for(int i =0; i<=str.length()-1 ; i++) {
            str = str.replace("\\s", "");
             str = str.replaceAll("\t", "");
             str = str.replaceAll("\n", "");
            System.out.println(str.charAt(i));
        }

            int OP = 0;

            switch(OP){
                case 0:
                    if(str.contains("<") && str.contains(">")){
                        System.out.println("Token name : relop");
                        System.out.println("Attribute value = NE");

                        break;

                    }
                case 1:
                    if(str.contains("<") && str.contains("=")){
                        System.out.println("Token name = relop");
                        System.out.println("Attribute value = LE");
                        break;
                    }

                case 2:
                    if(str.contains(">") && str.contains("=")){
                        System.out.println("Token name = relop");
                        System.out.println("Attribute value = GE");
                        break;
                    }

                case 3:
                    if(str.contains("<")){
                        System.out.println("Token name = relop");
                        System.out.println("Attribute value = LT");
                        break;
                    }
                case 4:
                    if(str.contains(">")){
                        System.out.println("Token name = relop");
                        System.out.println("Attribute value = GT");
                        break;
                    }
                case 5:
                    if(str.contains("==")){
                        System.out.println("Token name = relop");
                        System.out.println("Attribute value = EQ");
                        break;
                    }
                case 6:
                    if(str.contains("+")){
                        System.out.println("Token name = arop");
                        System.out.println("Attribute value = ADD");
                        break;
                    }
                case 7:
                    if(str.contains("-")){
                        System.out.println("Token name = arop");
                        System.out.println("Attribute value = SUB");
                        break;
                    }
                case 8:
                    if(str.contains("*")){
                        System.out.println("Token name = arop");
                        System.out.println("Attribute value = MUL");
                        break;
                    }
                case 9:
                    if(str.contains("/")){
                        System.out.println("Token name = arop");
                        System.out.println("Attribute value = DIV");
                        break;
                    }
                case 10:
                    if(str.contains("=")){
                        System.out.println("Token name = otop");
                        System.out.println("Attribute value = ASN");

                        if(str.startsWith("=")){
                            break;
                        }
                        else{
                        try{
                        String a[] = str.split(" ");
                        type = a[0];
                        lexeme = a[1];
                        value = a[3];
                        if(value.endsWith(";")){
                        value = value.replace(";", "");
                        if(type.contains("int") && value.matches("[-+][0-9]*")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("float") && value.matches("[0-9].[0-9]*")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("string") && value.matches("\"[A-Z a-z 0-9]*\"")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else if(type.contains("char") && value.matches("\'[A-Za-z0-9]\'")){
                            tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\t"+value);
                            System.out.println("Data Inserted in the table!");
                        }
                        else{
                            System.out.println("ERROR: Type MissMatch!");
                        }
                        }
                        else if(!value.endsWith(";")){
                            System.out.println("\nERROR: ; expected on line number " +lineNumber.getLineNumber() + " in the text file");
                        }
                        }
                        catch(Exception e){
                            System.out.println("Error: Try to declare variale with spaces e.g int a = 5;");
                        }
                        }
                        break;
                    }
//                case 11:
//                    if(str.contains("'")){
//                        System.out.println("Token name = otop");
//                        System.out.println("Attribute value = PRN");
//                        break;
//                    }
                case 12:
                    if(str.contains(";")){
                        System.out.println("Token name = otop");
                        System.out.println("Attribute value = LNTR");
                            if(str.startsWith(";")){
                            break;
                        }
                        String p[] = str.split(" ");
                        type = p[0];
                        lexeme = p[1];
                        if(lexeme.endsWith(";")){
                        System.out.println(p[1]);
                        lexeme = lexeme.replace(";", "");
                        tabledata.add(lexeme+"\t\tid\t\t"+type+"\t\tNull");
                        System.out.println("Data Inserted in the table!");
                        }
                        else if(!lexeme.endsWith(";")){
                            System.out.println("\nERROR: ; expected on line number " +lineNumber.getLineNumber() + " in the text file");
                        }
                        break;
                    }
                case 13:
                    if(str.contains("{")){
                        System.out.println("Token name = otop");
                        System.out.println("Attribute value = LBRC");
                        break;
                    }
                case 14:
                    if(str.contains("}")){
                        System.out.println("Token name = otop");
                        System.out.println("Attribue value = RBRC");
                        break;
                    }
                case 15:
                   if(str.contains("begin")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 1");
                        break;
                    }
                case 16:
                   if(str.contains("end")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 2");
                        break;
                    }
                case 17:
                   if(str.contains("if")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 3");
                        break;
                    }
                case 18:
                   if(str.contains("then")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 4");
                        break;
                    }
                case 19:
                   if(str.contains("else")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 5");
                        break;
                    }
                case 20:
                   if(str.contains("int")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 6");
                        break;
                   }
                case 21:
                   if(str.contains("float")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 7");
                        break;
                   }
                case 22:
                   if(str.contains("char")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 8");
                        break;
                    }
                case 23:
                   if(str.contains("string")){
                        System.out.println("Token name = Keyword");
                        System.out.println("Attribute value = 9");
                        break;
                    }
                case 24:
                        if(str.matches("[A-Za-z_][A-Za-z_0-9_]*")){
                            System.out.println("Token name = id");
                            tabledata.add(str+"\t\tid\t\tIdentifier\tNull");
                            System.out.println("Data Inserted in the table!");
                            break;
                    }
                case 25:
                    if(str.matches("[+0-9]*")){
                        System.out.println("Token name = uint");
                        tabledata.add("Null\t\tuint\t\tint\t\t"+str);
                        System.out.println("Data Inserted in the table!");
                        break;
                    }
                case 26:
                    if(str.matches("\"[A-Z a-z]*\"")) {
                        System.out.println("Token name = sliteral");
                        tabledata.add("Null\t\tsliteral\tstring\t\t"+str);
                        System.out.println("Data Inserted in the table!");
                        break;
                    }
                case 27:
                    if(str.matches("[0-9].[0-9]*")){
                        System.out.println("Token name = float const");
                        tabledata.add("Null\t\tfloat const\tfloat\t\t"+str);
                        System.out.println("Data Inserted in the table!");
                        break;
                    }
                case 28:
                    if(!str.contains("<>|>=|<=|<|>|==|+|-|/|*|'|;|{|}|=|begin|end|else|if|then|int|float|char|string") && !str.matches("\"[A-Z a-z]*\"") && !str.matches("\'[A-Za-z]\'")){
                        System.out.println("The unrecognized lexeme is :" +str);
                        System.out.println("Line Number :" + lineNumber.getLineNumber());
                        System.out.println("Error: unrecognized token found!");
                    } 
                case 29:
                    if(str.matches("\'[A-Za-z]\'")){
                        System.out.println("Token name = char const");
                        tabledata.add("Null\t\tchar const\tchar\t\t"+str);
                        System.out.println("Data Inserted in the table!");
                        break;
                    }
        }
    }

  }
}
ljo96ir5

ljo96ir51#

提取adding to a submethod,该子方法检查数据是否已存在:

private static void addData(Set<String> tabledata, String data)
{
    if ( tabledata.contains(data) )
    {
        System.out.println("Data Already Exists!");
        return;
    }

    tabledata.add(data);
    System.out.println("Data Inserted in the table!");
}

并这样称呼它:

case 10:
    ...
    if(type.contains("int") && value.matches("[-+][0-9]*")){
        addData(tabledata, lexeme+"\t\tid\t\t"+type+"\t\t"+value);
    }
    ...

相关问题