将元素从文本文件中单独添加到队列中

时间:2015-10-31 22:33:20

标签: java data-structures queue

我想在队列中添加一个名称(链接),一次从文本文件中添加一个名称。如果用户选择选项1,那么它应该从列表中取出下一个名称。

如果我想添加其他名称,案例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;
}

1 个答案:

答案 0 :(得分: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;


    }

}