首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileNotFoundException问题

FileNotFoundException问题
EN

Stack Overflow用户
提问于 2011-02-21 00:57:12
回答 2查看 710关注 0票数 1

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

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

问候

路易

代码语言:javascript
复制
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;   
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-21 01:05:56

出现此错误是因为缺少文件sub_total.txt

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

票数 3
EN

Stack Overflow用户

发布于 2011-02-21 02:20:26

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

但这样你就会得到NumberFormatException

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

但小计可以是多个字符的,(小计会随着项数的增加而增加)。

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

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

NumberFormatException格式的结果

解决方案:

将数据以String格式写入文件

代码语言:javascript
复制
PrintWriter txt = new PrintWriter(out);
txt.print(pennies);
txt.close();

而不是out.write(pennies);,并将数据读取为

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

记住,在运行程序之前,不要忘记在sub_total.txt中存储一个整数值

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5058476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档