我正在嘗試讀取一個文件,並打印出文件中有多少特定字,但它只是打印「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();
}
}
並且w帽子的文件內容是什麼樣子的? – MadProgrammer
白麪包圈 奶油芝士 蜜桃果凍 花生醬 卡布奇諾 肉桂緊縮百吉 藍莓果醬 普通咖啡 麥百吉餅 藍莓果醬 花生醬 飲食博士辣椒 – PrettyNovice
這是在Excel和所有在一列,但我不能在這裏放一列。 – PrettyNovice