0% found this document useful (0 votes)
12 views

Java project

The Java program prompts the user to enter a five-digit number and validates the input. It ensures the number is within the range of 10000 to 99999 and extracts each digit from the number. Finally, it displays the individual digits to the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java project

The Java program prompts the user to enter a five-digit number and validates the input. It ensures the number is within the range of 10000 to 99999 and extracts each digit from the number. Finally, it displays the individual digits to the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

JOptionPane;

public class digits {


public static void main(String[] args) {
int number = 0;
boolean valid = false;

// Loop until a valid five-digit number is entered


while (!valid) {
try {
String input = JOptionPane.showInputDialog("Enter a five-digit
number:");
if (input == null) {
// If user clicks cancel or closes the dialog
System.exit(0);
}

number = Integer.parseInt(input);

if (number >= 10000 && number <= 99999) {


valid = true;
} else {
System.out.println("Invalid input! Please enter a number that has
exactly five digits.");
}
} catch (NumberFormatException e) {
System.out.println("Invalid input! Please enter numeric digits only.");
}
}

// Extract digits
int digit1 = number / 10000;

int digit2 = (number / 1000) % 10;


int digit3 = (number / 100) % 10;
int digit4 = (number / 10) % 10;
int digit5 = number % 10;

// Display digits
String output = "The digits are:\n " + digit1 + "\t " + digit2 + "\t " + digit3 +
"\t " + digit4 + "\t " + digit5;
System.out.println(output);
}
}

You might also like