JOptionPane和读取整数 - 初学者Java

问题描述:

我目前有读取用户输入一行(用空格分隔)的月份,日期和年份的代码。这是代码。JOptionPane和读取整数 - 初学者Java

Scanner input = new Scanner(System.in); 
int day = 0; 
int month = 0; 
int year = 0; 

System.out.printf("enter the month, date, and year(a 2 numbered year). Put a space between the month, day, and year"); 
month = input.nextInt(); 
day = input.nextInt(); 
year = input.nextInt(); 

这工作得很好,第二部分是显示一个消息,如果月*日==一年,那么它是一个神奇的数字,如果不是,那么它是不是一个神奇的数字。它必须显示在一个对话框中。这是我的代码,它工作得很好。

if((day * month) == year) 
    { 
    String message = String.format("%s", "The date you entered is MAGIC!");//If the day * month equals the year, then it is a magic number 
    JOptionPane.showMessageDialog(null, message); 
    } 
    if((day * month) != year) 
    { 
    String message = String.format("%s", "The date you entered is NOT MAGIC!");//If the day * month does not equal the year, it is not a magic number 
    JOptionPane.showMessageDialog(null, message); 
    } 

我的问题是!!我怎样才能得到一个对话框,以便在控制台中以一行方式输入月份,日期和年份的输入。我在DrJava工作,而我所在的这本书的章节并没有帮助我进行这种具体的使用。任何帮助都会很棒。谢谢大家!

您可以使用以下采取用户输入

String word = JOptionPane.showInputDialog("Enter 3 int values"); 
String[] vals = word.split("\\s+"); // split the sting by whitespaces accepts regex. 
// vals[0] cast to int 
// convert string representation of number into actual int value 
int day = Integer.parseInt(vals[0]); // throws NumberFormatException 
// vals[1] cast to int 
// vals[2] cast to int 

split Java API

parseInt Java API

Java Regex Tutorial

+0

谢谢你给了一些代码。它似乎在工作,但你可以做一个快速解释如何。我只需要帮助理解String {} vals = word.split(“\\ s +”)和parseInt方法。 – ShiftyMcGrifty 2013-02-09 01:47:32

+0

@ShiftyMcGrifty我更新了答案,并添加了几个链接,这将给你更多的信息。 – Smit 2013-02-09 01:54:36

+0

非常感谢,我现在正在查看API。看着我的任务,我相信我的教授只是要求输出对话框,我能够得到。但学习这将在未来有用。再一次,谢谢! – ShiftyMcGrifty 2013-02-09 01:58:39

这里是一些代码,一切都在注释中描述:

// import statements 
import javax.swing.JOptionPane; 
// main class 
public class Main { 
    // main method 
    public static void main(String[] args) { 
     // get info 
     try { 
      Info info = new Info(); 
     } catch(Exception e) { 
      System.err.println("Error! " + e.getMessage()); 
     } 
     // do whatever with the info 
    } 
    // info class 
    static class Info { 
     // instance variables 
     public int day, month, year; 
     // constructor 
     public Info() throws Exception { 
      // get inputs 
      String[] inputs = JOptionPane.showInputDialog(null, 
       "Enter day, month, year").split(" "); 
      // not the right size 
      if(inputs.length != 3) { 
       throw new Exception("Not enough infomation was given!"); 
      } 
      // get values 
      day = Integer.parseInt(inputs[0]); 
      month = Integer.parseInt(inputs[1]); 
      year = Integer.parseInt(inputs[2]); 
     } 
    } 
} 

它有一个优雅的方式来通知你,如果出了什么问题,你需要的东西打包在一个方便的对象。

+0

我很欣赏这个帮助,而我的教授要求所有的东西都在同一个班,而主要的是,我明白你的目标。对未来绝对有帮助。 – ShiftyMcGrifty 2013-02-09 02:42:29

+0

@ShiftyMcGrifty很高兴能帮到你。 – Aaron 2013-02-09 03:07:36

有很多方法可以根据最终要实现的目标来解决问题。

JOptionPane允许您提供Object作为消息。如果此消息是String,则它将按原样呈现,但如果它是某种类型的Component,则会简单地将其添加到对话框中。这使得JOptionPane是一个非常强大的小API。

enter image description here

public class TestOptionPane07 { 

    public static void main(String[] args) { 
     new TestOptionPane07(); 
    } 

    public TestOptionPane07() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JTextField fldDay = new JTextField(3); 
       JTextField fldMonth = new JTextField(3); 
       JTextField fldYear = new JTextField(4); 
       JPanel message = new JPanel(); 
       message.add(fldDay); 
       message.add(new JLabel("/")); 
       message.add(fldMonth); 
       message.add(new JLabel("/")); 
       message.add(fldYear); 

       int result = JOptionPane.showConfirmDialog(null, message, "Enter Date", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
       if (result == JOptionPane.OK_OPTION) { 
        String sDay = fldDay.getText(); 
        String sMonth = fldMonth.getText(); 
        String sYear = fldYear.getText(); 
        JOptionPane.showMessageDialog(null, "You enetered " + sDay + "/" + sMonth + "/" + sYear); 

        try { 
         int day = Integer.parseInt(sDay); 
         int month = Integer.parseInt(sMonth); 
         int year = Integer.parseInt(sYear); 
         JOptionPane.showMessageDialog(null, "You enetered " + day + "/" + month + "/" + year); 
        } catch (Exception e) { 
         JOptionPane.showMessageDialog(null, "The values you entered are invalid"); 
        } 
       } 
      } 
     }); 
    } 
} 

更新

如果我打算用这样的事情,我也将使用一个DocumentFilter,以确保用户只能输入有效的值(例子here

但你也可以使用JSpinners

enter image description hereenter image description here

或者JComboBox

enter image description here

取决于它是什么你想达到...

+0

我很欣赏这个帮助!虽然这是我在编程课上的第四周,但我不认为这是我的教授对我们的期望。我不熟悉编写自己的方法或使用多个类的程序。但我现在明白了JOptionPane提供的复杂性和实用性。 – ShiftyMcGrifty 2013-02-09 02:39:57

+0

是的,我想很多人都没有意识到像JOptionPane这样的东西真的有多强大。为您的需求找到最佳解决方案! – MadProgrammer 2013-02-09 03:29:48