java - 添加 if 语句进行标记化

标签 java if-statement tokenize stringtokenizer

我正在尝试编写一个程序,该程序将使用插入、删除和排序等命令获取文本文件,并让它们从链接列表中插入或删除节点。到目前为止,我已经能够标记字符串并将它们写出来;但我也想使用 if 语句,无论文本行是说插入还是删除。这是我到目前为止所拥有的。感谢您的帮助。

(车道.txt)

         insert 1

         insert 7

         insert 5

         delete 7

         insert 2

         insert 4

         delete 5

代码:

导入java.io。; 导入java.util.;

类 TokenTest {

 public static void main (String[] args) {
    TokenTest tt = new TokenTest();
    tt.dbTest();
 }

 void dbTest() { 

    DataInputStream dis = null;
    String dbRecord = null;

    try { 

       File f = new File("lane.txt");
       FileInputStream fis = new FileInputStream(f); 
       BufferedInputStream bis = new BufferedInputStream(fis); 
       dis = new DataInputStream(bis);

       // read the first record of the database
       while ( (dbRecord = dis.readLine()) != null) {

          StringTokenizer st = new StringTokenizer(dbRecord, " ");
          String action = st.nextToken();
          String key = st.nextToken();


          System.out.println("Action:  " + action);
            if(action == "insert")
            {
                System.out.println("holla");
            }
          System.out.println("Key Value:   " + key);
             if(action == "delete")
            {
                System.out.println("holla");
            }
          System.out.println(" ");


       }

    } catch (IOException e) { 
       // catch io errors from FileInputStream or readLine() 
       System.out.println("Uh oh, got an IOException error: " + e.getMessage()); 

    } finally { 
       // if the file opened okay, make sure we close it 
       if (dis != null) {
          try {
             dis.close();
          } catch (IOException ioe) {
             System.out.println("IOException error trying to close the file: "); 
          }

       } // end if

    } // end finally

 } // end dbTest

}//结束类(class)

输出:

操作:插入 键值:1

操作:插入 键值:7

操作:插入 键值:5

操作:删除 键值:7

操作:插入 键值:2

操作:插入 键值:4

操作:删除 键值:5

最佳答案

字符串应该使用等于进行比较:

if (action.equals("insert")) { // etc.

关于java - 添加 if 语句进行标记化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864368/

相关文章:

java - 一些数组操作

c - 我怎样才能更好地组织这两个连续的 IF 语句?

JavaMail message.getSubject().equals 给出 NullPointerException

c++ - 类不存在默认构造函数的错误

java - 更新了表,但没有返回正确的值

java - org.json.JSONObject 是否违反了 Object.equals() 契约(Contract)?

java - 生成特定范围内的带有标准偏差的随机数?

java - 在java中使用方法返回

java - 关于正则表达式中字符的恰好 n 次出现

c++ - 如何在 C++ 中标记字符串?