使用BufferedReader按任意键

时间:2016-12-22 03:37:25

标签: java file-io

我创建了一个名为Product.java的java文件。我还创建了一个名为Items.txt的文本文件。基本上,当用户使用顺序搜索输入单词时,可以从Items.txt中搜索数据。我的主要问题是当我输入3来显示所有记录或输入x来退出程序时,它会继续循环。但我不知道如何解决这个问题。任何人都可以为我解决这个问题吗?

Items.txt

1000|Cream of Wheat|Normal Size|Breakfast|NTUC|5|3.00
1001|Ayam Brand|Small Size|Canned|NTUC|4|4.00

Product.java

import java.io.*;
import java.util.*;

public class Product {


    public static void main(String[] args) {

        ArrayList<Item> prdct = new ArrayList<Item>();
        String inFile = "items.txt";
        String line = "";
        FileReader fr = null;
        BufferedReader br = null;
        StringTokenizer tokenizer;

        int quantity;
        String id, brandname, desc, category, supplier;
        float price;

        try{
            fr = new FileReader(inFile);
            br = new BufferedReader(fr);

            line = br.readLine();

            while(line!=null)
            {
                tokenizer = new StringTokenizer(line,"|");

                id = tokenizer.nextToken();
                brandname = tokenizer.nextToken();
                desc = tokenizer.nextToken();
                category = tokenizer.nextToken();
                supplier = tokenizer.nextToken();
                quantity = Integer.parseInt(tokenizer.nextToken());
                price = Float.parseFloat(tokenizer.nextToken());

                Item itm = new Item(id,brandname,desc,category,supplier,quantity,price);
                prdct.add(itm);

                line = br.readLine();
            }
            br.close();     
        }
        catch(FileNotFoundException e){
            System.out.println("The file " + inFile + " was not found.");
        }
        catch(IOException e){
            System.out.println("Reading error!");
        }
        finally
        {
            if (fr!=null){
                try
                {
                    fr.close();
                }
                catch(IOException e)
                {
                    System.out.println("Error closing file!");
                }
            }
        }
        String INPUT_PROMPT = "\nPlease enter 3 to display all records, 4 to insert record, 5 to remove old records " + "or enter 'x' to quit.";
        System.out.println(INPUT_PROMPT);

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

            while(reader != null)
            {
                for(int i=0; i<prdct.size(); i++)
                {
                     if(prdct.get(i).id.contains(line) || prdct.get(i).brandname.contains(line) || prdct.get(i).desc.contains(line)
                        || prdct.get(i).category.contains(line) || prdct.get(i).supplier.contains(line))
                     {
                            System.out.println(prdct.get(i));
                     }
                     System.out.println(INPUT_PROMPT);
                     line = reader.readLine();

                }
            }


            while("3".equals(line))
            {
                 for(int i=0; i<prdct.size(); i++)
                 {
                     System.out.println(prdct.get(i));
                 }
                 System.out.println(INPUT_PROMPT);
                 line = reader.readLine();

            }

            while(!line.equals("x"))
            {
                  System.out.println(INPUT_PROMPT);
                  line=reader.readLine();
            }
        }
        catch(Exception e){
            System.out.println("Input Error!");
        }
    }

}

4 个答案:

答案 0 :(得分:1)

问题在于这个循环:

        while(reader != null)
        {
            for(int i=0; i<prdct.size(); i++)
            {
                 if(prdct.get(i).id.contains(line) || prdct.get(i).brandname.contains(line) || prdct.get(i).desc.contains(line)
                    || prdct.get(i).category.contains(line) || prdct.get(i).supplier.contains(line))
                 {
                        System.out.println(prdct.get(i));
                 }
                 System.out.println(INPUT_PROMPT);
                 line = reader.readLine();

            }
        }

它继续循环,而读者不是空的,它永远不会。你可能想尝试更好地检查一些更适合你问题的东西,可能:

  • while(!line.equals(&#34; 3&#34;))
  • 虽然(line.equals(&#34;!X&#34))
  • while(line!= null)

否则,即使有&#39; x&#39;,&#39; 3&#39;或者根本没有,仍然(读者!= null),因此循环是无限的。

答案 1 :(得分:0)

我怀疑换行符是导致比较失败的原因。

而不是检查是否:

"3".equals(line)

尝试:

"3".equals(line.trim())

同样适用于以下比较。

答案 2 :(得分:0)

尝试更改此内容..

       line = reader.readLine();

       while(reader != null)
       {

到此..

       line = reader.readLine();

       while(line != null)
       {

你正在读取的循环不是null,它总是如此。

答案 3 :(得分:0)

你必须定义这些功能:

public void showAllRecords() {
   // show all record here
}
public void insertRecord() {
   // insert record here
}
public void removeRecord() {
   // insert record here
}
public void exit() {
   // insert record here
}

然后

do{
     System.out.println(INPUT_PROMPT);
    switch(line)
    {
      case "3":
           showAllRecords();
           break;
      case "4":
          insertRecord();
          break;
      case "5":
         removeRecord();
    }
}while(!line.equals('x'));