0

挿入、削除、並べ替えなどのコマンドを含むテキストファイルを取得し、リンクリストからノードを挿入または削除するプログラムを作成しようとしています。これまでのところ、文字列をトークン化して書き出すことができました。ただし、テキスト行に挿入するか、削除するかを示すifステートメントも使用します。これは私がこれまでに持っているものです。助けてくれてありがとう。

(lane.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

}//クラスを終了

出力:

アクション:キー値の挿入:1

アクション:キー値の挿入:7

アクション:キー値の挿入:5

処置:キー値を削除してください:7

アクション:キー値を挿入します:2

アクション:キー値の挿入:4

処置:キー値を削除してください:5

4

2 に答える 2

3

文字列は次を使用して比較する必要がありますequals

if (action.equals("insert")) { // etc.
于 2012-06-02T17:18:11.983 に答える
0

Java 7 が導入されたので、switch case で文字列を使用できると思います。

Java 7 では、次の構文が許可されるようになったと思います。

String s = ....;
switch(s)
{
   case "insert": 
      break;
   default:
      break;
}
于 2012-06-02T17:23:21.823 に答える