【问题标题】:What is the correct way to structure a try catch statement to only allow integers构造try catch语句以仅允许整数的正确方法是什么
【发布时间】:2014-07-03 03:17:59
【问题描述】:

我遇到的问题是程序显示这样

1 请输入姓名 红色的 请输入本次活动的价格

红色

请输入本次活动的价格

40

请输入活动日期

请输入此活动的可用门票数量。

它完全跳过了日期。如何编辑我的代码以便正确输入数据。

任何提示和建议都会很棒。我做错了什么?

谢谢

我的代码是

案例一:

 char ans;
    do{                 
        Event event = new Event();
        out.println("Please Enter the name ");
        event.setEvent(input.next());
        input.nextLine();

        do {
            try {
                out.println("Please Enter the price of this event\n");
                event.setprice(input.nextDouble());
            } catch (InputMismatchException e) {
                out.println("Please Enter the price of this event\n");
            }
            input.nextLine();
        } while (!input.hasNextDouble());

        out.println("Please Enter the date of this event\n");
        event.setdate(input.next());

        out.println("Please enter the amount of tickets available for this event.\n");
                event.setavailability(input.nextInt());

        out.println("Please enter");
        event.setvenue(input.next());
        Events.add(event);

        out.println("Would you like to add a new event?");
        String answer = input.next();
        ans = answer.charAt(0);

        while (ans== 'y');      
            out.println (Events.get(0));
            break;
        }

【问题讨论】:

  • 虽然 StackOverflow 是您回答许多编程问题的地方,但它不是您为您完成作业的地方。如果你愿意的话,我的建议是质疑你对输入的调用以及你是想从中消费还是测试它。
  • 您似乎将input.next()(及其表亲)与input.nextLine() 混合在一起——您不应该那样做。从控制台收集输入时,您应该使用input.nextLine(),然后解析用户输入的内容。如果你这样做,事情会变得更顺利。
  • 暂时忘记异常。正如 Jared 所建议的,您需要了解 Scanner 上各种方法之间的区别。编写一些玩具程序并玩一会儿。
  • 没有例外我的代码工作正常。例如'code' case 1: char ans;做{事件事件=新事件(); out.println("请输入姓名"); event.setEvent(input.next()); input.nextLine(); out.println("请输入本次活动价格\n"); event.setprice(input.nextDouble()); out.println("请输入本次活动的日期\n"); event.setdate(input.next()); 'code' 我应该以不同的方式输入吗?

标签: java eclipse error-handling try-catch case


【解决方案1】:

以下代码显示了一个带有 JLabel、一个 TextField 和一个 JButtonJFrame。如果您输入一个整数,它会显示一个 JOptionPane 说... 接受。否则它将显示带有错误消息的 JOptionPane。我想您可能希望处理 NumberFormatException。您应该更新您的问题,否则它将被否决。以下代码处理 NumberFormatException

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ExceptionTest extends JFrame implements ActionListener {

    private JTextField jtf;

    public ExceptionTest() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel jp = new JPanel();
        add(jp);
        this.setSize(300, 200);
        JLabel lbl = new JLabel("Enter price");
        jp.add(lbl);
        jtf = new JTextField(9);
        jp.add(jtf);
        JButton btn = new JButton("Click to check");
        btn.addActionListener(this);
        jp.add(btn);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        new ExceptionTest().setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        try {
            Integer.parseInt(jtf.getText());
            JOptionPane.showMessageDialog(this, "Number accepted");
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this,
                    "This text field accept only integers", "Invalid Input",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}

【讨论】:

  • 为什么不扩展 JDialog 而不是 JFrame?
  • 使用 JFrame 代替 JDialog.. 是否会降低性能?
  • 否,但 JDialog 用于您使用 JFrame 创建的对话框。
猜你喜欢
  • 2011-06-03
  • 2012-11-20
  • 2019-09-09
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 2018-05-23
  • 1970-01-01
  • 2010-10-14
相关资源
最近更新 更多