JOptionPane and reading integers - Beginner Java

前端 未结 3 1519
深忆病人
深忆病人 2020-12-07 05:10

I currently have code that reads the month, date, and year a user enters in one line (separated by spaces). Here is the code.

Scanner input = new Scanner(Sys         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 05:35

    You can make use of following to take user input

    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

提交回复
热议问题