2017-02-02 14 views
1

В основном нужно выполнить последовательного поиск по списку элементов слова либо Категории или Диетических и производить перечень информации в следующем формате вывод:Проблемы с последовательным поиском

Формат данных:

ID|Name|Description|Category|Dietary|Quantity|Unit Price 

Выход:

Please enter the number: 
1 to search category 
2 to search dietary 
3 to display all records 
4 to insert record 
5 to remove old records 
or enter '6' to quit. 

1 

Please enter the number: 
1 to search category 
2 to search dietary 
3 to display all records 
4 to insert record 
5 to remove old records 
or enter '6' to quit. 

rice 
1004|Premium Fragrant Rice|Large Size|Rice|Organic|2|9.5 

Please enter the number: 
1 to search category 
2 to search dietary 
3 to display all records 
4 to insert record 
5 to remove old records 
or enter '6' to quit. 

Но моя проблема в том, что когда я запускаю свою программу, я ввожу 1 в меню пользователя для поиска категории, тогда она никогда не отображает список информации о товарах. Но я не уверен, как исправить код на линии 81?

Мой выход:

Please enter the number: 
    1 to search category 
    2 to search dietary 
    3 to display all records 
    4 to insert record 
    5 to remove old records 
    or enter '6' to quit. 

    1 

    Please enter the number: 
    1 to search category 
    2 to search dietary 
    3 to display all records 
    4 to insert record 
    5 to remove old records 
    or enter '6' to quit. 

    rice 

    Please enter the number: 
    1 to search category 
    2 to search dietary 
    3 to display all records 
    4 to insert record 
    5 to remove old records 
    or enter '6' to quit. 

Java:

String INPUT_PROMPT = "\nPlease enter the number:\n" + 
       "1 to search category" 
       + "\n2 to search dietary" + "\n3 to display all records" + "\n4 to insert record" + "\n5 to remove old records " + "\nor enter '6' to quit." +"\n"; 
     System.out.println(INPUT_PROMPT); 

     try 
     {   
      BufferedReader reader = new BufferedReader 
        (new InputStreamReader (System.in)); 
      line = reader.readLine(); 


      while(!line.equals("6")) 
      {  
       switch(line) 
       { 
        //Search word for Category 
        case "1": <-----Line 81 
        int i=0; 
        while(i<prdct.size()) 
        { 
         if(prdct.get(i).category.contains(line)) 
         { 
          System.out.println(prdct.get(i)); 
         } 
         i++; 
        } 
        if(i == 0) 
        { 
         System.out.println("Record not found"); 
        } 
        break; 

        case "3": 
        for(int h=0; h<prdct.size(); h++) 
        { 
         System.out.println(prdct.get(h)); 
        } 
        break; 
       } 

       System.out.println(INPUT_PROMPT); 
       line = reader.readLine(); 

      } 

     } 
     catch(Exception e){ 
      System.out.println("Input Error!"); 
     } 

ответ

0

Посмотрите на приведенный ниже код, строка за строкой:

line = reader.readLine(); 

Читаешь строки первой.

while(!line.equals("6")) 
{  
    switch(line) 
    { 
     //Search word for Category 
     case "1": 

Затем вы проверяете строку, которую вы читаете, чтобы увидеть, введен ли пользователь «1».

 int i=0; 
     while(i<prdct.size()) 
     { 
      if(prdct.get(i).category.contains(line)) 

Затем вы просматриваете список для введенной пользователем строки.

Вы никогда не вводите новый пользовательский ввод! Поэтому вместо поиска категории, которую хочет пользователь, вы по-прежнему ищете строку «1». Повторите попытку пользователя, чтобы устранить эту проблему. Просто добавьте еще reader.readLine(), прежде чем искать в списке:

 line = reader.readLine(); 
     int i=0; 
     while(i<prdct.size()) 
     { 
      if(prdct.get(i).category.contains(line)) 

 Смежные вопросы

  • Нет связанных вопросов^_^