Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.1k points)

This is the code that reads the month, year, and date when a user enters in one line.

Scanner input = new Scanner(System.in);

int day = 0;

int month = 0;

int year = 0;

System.out.printf("enter the month, date, and year(only last two digits of year). Please insert a space between the month, day, and year");

month = input.nextInt();

day = input.nextInt();

year = input.nextInt();

This works completely fine, now the other part is displaying a message, if month*day==year, it is a magical number. It has to be displayed in a dialog box. This code is also working fine. Here is the code:

  if((day * month) == year)

  {

    String message = String.format("%s", "It is a magical date");//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", "It is not a magical date");//If the day * month does not equal the year, it is not a magic number

    JOptionPane.showMessageDialog(null, message);

  }

I want to know how to get a dialog box to take inputs of the month, date, and year?

1 Answer

0 votes
by (11.7k points)

This code will work to take the user input:

String word = JOptionPane.showInputDialog("Enter 3 int values");

String[] vals = word.split("\\s+"); // split the string 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

If you want to become an expert in Java, check out the Java Course provided by Intellipaat.

Browse Categories

...