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

时间:2023-02-06 12:20:45

I am trying to read a file, and print out how many of a specific word are in the file, but it is just printing "0". It was working, and I changed the contents of the file, then it broke and it won't work now.

我正在尝试读取文件,并打印出文件中有多少特定单词,但它只是打印“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 个解决方案

#1


Probably you would be having white spaces around the word. You could do one of the follwoing.

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

Instead of

line.equalsIgnoreCase("White Bagel")

try using

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

or if you want to account for case insensitive then

或者如果你想考虑不区分大小写的话

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

I opt option 1, if I don't account for case sensitivity

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

#2


You can use java.util.Scanner class to break data in tokens from any input stream. You should visit this link for how scanner works

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

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

#1


Probably you would be having white spaces around the word. You could do one of the follwoing.

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

Instead of

line.equalsIgnoreCase("White Bagel")

try using

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

or if you want to account for case insensitive then

或者如果你想考虑不区分大小写的话

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

I opt option 1, if I don't account for case sensitivity

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

#2


You can use java.util.Scanner class to break data in tokens from any input stream. You should visit this link for how scanner works

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

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