我的程序不会读取我的文件

时间:2015-05-04 04:07:31

标签: java

我正在尝试读取文件,并打印出文件中有多少特定单词,但它只是打印“0”。它工作,我改变了文件的内容,然后它破了,它现在不起作用。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.io.*;

public class TotalsGUI extends JFrame
{
   private BagelPanel bagels;     
   private ToppingPanel toppings; 
   private CoffeePanel coffee;    
   private GreetingPanel banner;  
   private JPanel buttonPanel;    
   private JButton calcButton;   
   private JButton exitButton;   
   private final double TAX_RATE = 0.06;
   int whiteCount = 0;
   int wheatCount = 0;

   public TotalsGUI() throws IOException
   {
      setTitle("Totals Calculator");

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setLayout(new BorderLayout());

      banner = new GreetingPanel();
      bagels = new BagelPanel();

      buildButtonPanel();

      add(banner, BorderLayout.NORTH);
      add(bagels, BorderLayout.WEST);
      add(buttonPanel, BorderLayout.SOUTH);

      pack();
      setVisible(true);
   }

   private void buildButtonPanel()
   {
      // Create a panel for the buttons.
      buttonPanel = new JPanel();

      // Create the buttons.
      calcButton = new JButton("Calculate");

      // Register the action listeners.
      calcButton.addActionListener(new CalcButtonListener());

      // Add the buttons to the button panel.
      buttonPanel.add(calcButton);
   }

   private class CalcButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         String fileName = "receipt.csv";
         String line = null;

         try 
         {
            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null)
            {
               if (line.equalsIgnoreCase("White Bagel"))
               {
                  whiteCount++;
               }
               else if (line.equalsIgnoreCase("Wheat Bagel"))
               {
                  wheatCount++;
               }
            }

            bufferedReader.close();            
         }
         catch(FileNotFoundException ex) 
         {
            System.out.println(
               "Unable to open file '" + 
               fileName + "'");
         }
         catch(IOException ex) 
         {
            System.out.println(
               "Error reading file '" 
               + fileName + "'");
         }

         System.out.printf("White Bagels: %d\n", whiteCount);
         System.out.printf("Wheat Bagels: %d", wheatCount);
      }
   }

   public static void main(String[] args) throws IOException
   {
      new OrderCalculatorGUI();
   }
}

2 个答案:

答案 0 :(得分:1)

可能你会在这个词周围留下空白。 你可以做其中一个。

而不是

line.equalsIgnoreCase("White Bagel")

尝试使用

if(line.indexOf("White Bagel")>-1)

或者如果您想要考虑不区分大小写,那么

 line.trim().equalsIgnoreCase("White Bagel")

我选择选项1,如果我不考虑区分大小写

答案 1 :(得分:-1)

您可以使用java.util.Scanner类来破坏来自任何输入流的标记中的数据。您应该访问此链接了解扫描仪的工作原理

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html