文件未找到异常问题

发布于 2024-10-18 10:07:15 字数 4483 浏览 6 评论 0原文

我刚刚读完“Java for Dummies”,并开始创建一个简单的 POS 程序。我一直无法让程序做我想做的事!我有两个 actionListeners 链接到以下每个按钮:“Amigos”和“Fosters”。我还有两个文本字段,一个显示单种饮料的价格,另一个用于小计。我确实有将同一种饮料的倍数相加的小计,但没有将“Amigos”与“Fosters”相加。这是通过尝试共享小计变量来编程的。我尝试通过读取和写入单个文本文件来解决我的业余 java 编程问题,但这对我来说也很困难!下面是我尝试实现读写工作的代码。

这是我的第一个 Java 程序,所以请原谅我弄错的格式、标点符号和 Java 约定。也请原谅我的评论不足。非常感谢任何建议!

问候

路易斯

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.io.FileNotFoundException;

class till_v2 {

    public static void main(String args[]) {

        JFrame frame;
        Container content_pane;
        JTextField textField, subTotal;
        JButton b1Amigos, b2Fosters;
        FlowLayout layout;

        frame = new JFrame();
        frame.setTitle("Louis' Till");

        content_pane = frame.getContentPane();

        textField = new JTextField("Price displayed here.",15);
        subTotal = new JTextField("Sub-Total.", 5);

        b1Amigos = new JButton("Amigos");
        b1Amigos.addActionListener(new AmigosAL(textField));
        b1Amigos.addActionListener(new subTotalAmigosUD(subTotal));

        b2Fosters = new JButton("Fosters");
        b2Fosters.addActionListener(new FostersAL(textField));
        b2Fosters.addActionListener(new subTotalFostersUD(subTotal));

        content_pane.add(textField);
        content_pane.add(subTotal);
        content_pane.add(b1Amigos);
        content_pane.add(b2Fosters);
        layout = new FlowLayout();
        content_pane.setLayout(layout);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }
}

class subTotalFostersUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalFostersUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //The problem could be here!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 320;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("1!");
        }
    }
}

class subTotalAmigosUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalAmigosUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //Same problem as above!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 330;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("2!");
        }
    }
}

class AmigosAL implements ActionListener {

    JTextField textField;

    AmigosAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.30");
    }
}


class FostersAL implements ActionListener {

    JTextField textField;

    FostersAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.20");
    }
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.*;

//The problem could be here!
public class SUBTOTAL {
    BufferedReader in;
    BufferedWriter out;
    int pennies;
    int itemPrice;
    public double sub_total;        

    public double SUBTOTAL(int itemPrice) throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("sub_total.txt"));
            pennies = Integer.parseInt(in.readLine());
            pennies = pennies + itemPrice;
            in.close();
        }

        catch(IOException e) {
            System.out.println("3!");
        }

        try {
            out = new BufferedWriter(new FileWriter("sub_total.txt"));
            out.write(pennies);
            out.close();
        }

        catch(IOException e){
            System.out.println("4!");
        }

        sub_total = pennies;
        sub_total = sub_total / 100;
        return sub_total;   
    }
}

I've just finished reading 'Java for Dummies' and have begun to create a simple POS program. I have been having trouble getting the program to do what I want it to do! I have two actionListeners linked to each of the following buttons, 'Amigos' and 'Fosters'. I also have two text fields, one showing the price of the individual drink and then other used for a sub-total. I did have the sub-total working to add up multiples of the same drink, but not an 'Amigos' with a 'Fosters'. This was programmed by trying to share a sub-total variable. I tried to work around my amateur java programming by reading and writing to a single text file but that is proving difficult for me as well! Below is my code trying to implement my read and write work around.

This is my first ever Java program so please forgive the format, punctuation and Java conventions I have got wrong. Also forgive my lack of comments. Any advice is greatly appreciated!

Regards

Louis

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.io.FileNotFoundException;

class till_v2 {

    public static void main(String args[]) {

        JFrame frame;
        Container content_pane;
        JTextField textField, subTotal;
        JButton b1Amigos, b2Fosters;
        FlowLayout layout;

        frame = new JFrame();
        frame.setTitle("Louis' Till");

        content_pane = frame.getContentPane();

        textField = new JTextField("Price displayed here.",15);
        subTotal = new JTextField("Sub-Total.", 5);

        b1Amigos = new JButton("Amigos");
        b1Amigos.addActionListener(new AmigosAL(textField));
        b1Amigos.addActionListener(new subTotalAmigosUD(subTotal));

        b2Fosters = new JButton("Fosters");
        b2Fosters.addActionListener(new FostersAL(textField));
        b2Fosters.addActionListener(new subTotalFostersUD(subTotal));

        content_pane.add(textField);
        content_pane.add(subTotal);
        content_pane.add(b1Amigos);
        content_pane.add(b2Fosters);
        layout = new FlowLayout();
        content_pane.setLayout(layout);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }
}

class subTotalFostersUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalFostersUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //The problem could be here!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 320;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("1!");
        }
    }
}

class subTotalAmigosUD implements ActionListener {

    JTextField subTotal;
    int itemPrice;
    double sub_total;
    SUBTOTAL SUBTOTALobject = new SUBTOTAL();

    subTotalAmigosUD(JTextField subTotal) {
        this.subTotal = subTotal;
    }
    //Same problem as above!
    public void actionPerformed(ActionEvent e) {
        try {
            itemPrice = 330;
            sub_total = SUBTOTALobject.SUBTOTAL(itemPrice);
            subTotal.setText("£"+sub_total);
        }

        catch (FileNotFoundException err) {
            System.out.println("2!");
        }
    }
}

class AmigosAL implements ActionListener {

    JTextField textField;

    AmigosAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.30");
    }
}


class FostersAL implements ActionListener {

    JTextField textField;

    FostersAL(JTextField textField) {
        this.textField = textField;
    }

    public void actionPerformed(ActionEvent e) {
        textField.setText("£3.20");
    }
}

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.*;

//The problem could be here!
public class SUBTOTAL {
    BufferedReader in;
    BufferedWriter out;
    int pennies;
    int itemPrice;
    public double sub_total;        

    public double SUBTOTAL(int itemPrice) throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("sub_total.txt"));
            pennies = Integer.parseInt(in.readLine());
            pennies = pennies + itemPrice;
            in.close();
        }

        catch(IOException e) {
            System.out.println("3!");
        }

        try {
            out = new BufferedWriter(new FileWriter("sub_total.txt"));
            out.write(pennies);
            out.close();
        }

        catch(IOException e){
            System.out.println("4!");
        }

        sub_total = pennies;
        sub_total = sub_total / 100;
        return sub_total;   
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2024-10-25 10:07:15

您收到此错误是因为您缺少文件sub_total.txt

请使用所需内容创建此文件。在您拥有 .class 文件的同一文件夹中。

you are getting this error because you are missing file sub_total.txt

create this file with desired content. In the same folder where you are having .class file.

暖心男生 2024-10-25 10:07:15

正如 Vivek 所说,它应该可以解决你的问题。

但随后你会得到 NumberFormatException

out.write(pennies);int 数据类型写入文件,指定字符 待写。

但小计可以超过一个字符(随着项目数量的增加,小计也会增加)。

当小计超过一个字符时,它会写入垃圾值sub_total.txt 文本文件中

Integer.parseInt(in.readLine()); 尝试将数据读取为 String 并将其解析为 int ,这

会导致 NumberFormatException


解决方案:

将数据作为 String

PrintWriter txt = new PrintWriter(out);
txt.print(pennies);
txt.close();

而不是 out.write(pennies) 写入文件);
并将数据读取为

pennies = Integer.parseInt(in.readLine());


请记住,在运行程序之前,不要忘记在 sub_total.txt< 中存储整数值/代码>

As Vivek said it should solve your problem.

But then you will get NumberFormatException

out.write(pennies); writes to file in int data type specifying a character to be written.

But subtotal can be more than one character,(subtotal increases as you increase the no of items).

When the subtotal is more than a character it writes junk values in the sub_total.txt textfile

Integer.parseInt(in.readLine()); tries to read the data as String and parse it into a int which

results in NumberFormatException


The Solution:

Write the data to file as String

PrintWriter txt = new PrintWriter(out);
txt.print(pennies);
txt.close();

instead of out.write(pennies);
and read the data as

pennies = Integer.parseInt(in.readLine());


And remember before you run your program, don't forget to store an integer value in sub_total.txt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文