2012-06-02 4 views
0

삽입, 삭제 및 정렬과 같은 명령을 사용하여 텍스트 파일을 가져 와서 링크 목록에서 노드를 삽입하거나 삭제하도록하는 프로그램을 작성하려고합니다. 지금까지 나는 문자열을 토큰 화하고이를 쓸 수있었습니다. 텍스트 줄에 삽입할지 또는 삭제할지 여부에 관계없이 if 문을 사용하려고합니다. 이것은 내가 지금까지 가지고있는 것이다. 어떤 도움을 주셔서 감사합니다.토큰화할 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 키 값을 삽입

작업 : inse RT 키 값 : 5

작업 : 7

작업 : : 키 값을 삭제 2

액션 : 4

작업 : : 키 값을 삽입 키 값을 삽입 삭제 키 값 : 5

+0

중복 가능성 (http://stackoverflow.com/questions/767372/java-string-equals :

I 자바 7 추측 이제 다음 구문을 허용 -versus) – Jeffrey

답변

3

equals :

01을 사용하여 문자열을 비교해야합니다.
if (action.equals("insert")) { // etc. 
+0

@mprabhat'action'은 OP가 파일에서 줄을 읽는 데 사용하는 코드에 따라 null이 될 수 없습니다. –

+0

모두에게 감사드립니다! .equals를 사용하여 문자열을 비교하고 해결했습니다. –

0

현재 자바 7을 사용하고 있으므로 스위치 케이스에 문자열을 사용할 수 있다고 생각합니다. [자바 String.equals 대 ==]의

String s = ....; 
switch(s) 
{ 
    case "insert": 
     break; 
    default: 
     break; 
} 
+0

감사합니다. 나는 그것을 명심해야합니다. –