2015-10-31 62 views
0

我想從一個文本文件中一次添加一個名稱到一個隊列(鏈接),一個名稱。如果用戶選擇選項1,那麼它應該從列表中取出下一個名稱。從文本文件中單獨添加元素到隊列

如果我想添加其他名稱,則不會讓我輸入另一個選項。

int choice = console.nextInt(); 

    FileReader names = new FileReader("Customer.txt"); 

    Scanner lookup = new Scanner(names); 

    Queue a = new Queue(); 


    String customerName; 
             // This loop was just to verify that 
    while(lookup.hasNextLine()){  // It was actually reading 

    customerName = lookup.next(); 
    System.out.println(customerName); 

    } 


    while(true){ 

     switch(choice){ 

    case 1: 
      customerName = lookup.next();//For some reason its not giving me 
      a.enqueue(customerName); // The choice to enter another number 
        break;   //even though I made the case while(true) 

    case 2: 

        break; 

    case 3: 

        break; 

    case 4: 

        break; 

    case 5: 

        break; 

    case 6: 
       System.out.println(" Exiting......"); 
     break; 


    default: 

     continue; 


    } 

    break; 
} 
+2

爲什麼會這樣讓你輸入的東西嗎?您正在從文件中讀取文件,而不是從控制檯讀取。你只能讀取用戶的一個輸入。 – Tom

+1

你的開關塊後有一個「break」,它終止了while循環。 – badjr

回答

1

這裏的問題是switch語句後有一箇中斷。這會導致您的代碼在switch語句執行一次後跳出while循環。

的解決方法是刪除了突破,因爲這樣:

while(true){ 

     switch(choice){ 

    case 1: 
      customerName = lookup.next();//For some reason its not giving me 
      a.enqueue(customerName); // The choice to enter another number 
        break;   //even though I made the case while(true) 

    case 2: 

        break; 

    case 3: 

        break; 

    case 4: 

        break; 

    case 5: 

        break; 

    case 6: 
       System.out.println(" Exiting......"); 
     break; 


    default: 

     continue; 


    } 

} 
相關問題