2013-02-09 115 views
1

我目前有讀取用戶輸入一行(用空格分隔)的月份,日期和年份的代碼。這是代碼。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工作,而我所在的這本書的章節並沒有幫助我進行這種具體的使用。任何幫助都會很棒。謝謝大家!

回答

2

您可以使用以下采取用戶輸入

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

2

這裏是一些代碼,一切都在註釋中描述:

// 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

6

有很多方法可以根據最終要實現的目標來解決問題。

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