-3

I am trying to complete an assignment, and I have almost fully completed it but I am have some trouble figuring out the last part.

How to: Check whether the number you received has five digits or not. Terminate gracefully if it doesn’t by notifying that the number should have 5 digits.

I've tried some stuff but I cannot get it to check correctly.

Here is my code:

import java.util.Scanner;

public class Five
{
  public static void main( String args[] )
{
    Scanner input = new Scanner( System.in );

    int number;
    int digit1;
    int digit2;
    int digit3;
    int digit4;
    int digit5;

    System.out.print( "Enter a five digit integer: " );
    number = input.nextInt();

    digit1 = number / 10000;
    digit2 = number % 10000 / 1000;
    digit3 = number % 10000 % 1000 / 100;
    digit4 = number % 10000 % 1000 % 100 / 10;
    digit5 = number % 10000 % 1000 % 100 % 10;

    System.out.printf( "Digits in %d are %d %d %d %d %d/n",
        number, digit1, digit2, digit3, digit4, digit5 );

   }
}

Thank you

JoeKig2
  • 1
  • 2

2 Answers2

1

You could ensure that the input is valid by doing a simple check of the user input like so

//take the user input as a string first
String userInput = input.next();
//then check the 
if(userInput.length() == 5 && userInput.matches("\\d\\d\\d\\d\\d"))
{
    number = Integer.parseInt(userInput);
    digit1 = number / 10000;
    digit2 = number % 10000 / 1000;
    digit3 = number % 10000 % 1000 / 100;
    digit4 = number % 10000 % 1000 % 100 / 10;
    digit5 = number % 10000 % 1000 % 100 % 10;

    System.out.printf( "Digits in %d are %d %d %d %d %d/n",
    number, digit1, digit2, digit3, digit4, digit5 );
}
else
{
     System.out.println(userInput + " is not a valid 5 digit number");
}
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
0

By all means, keep your current code and only submit something you wrote and feel comfortable with.

Using regex

I don't know if you know regular expression or if you are even allowed to use them. You could do something like this:

String userInput = input.next().trim();

if (userInput.matches("\\d{5}")) {
    String[] split = userInput.split("(?<=[0-9])");

    System.out.printf("Digits in %s are", userInput);
    for (int i = 0; i < split.length; i++) {
        System.out.printf(" %s", split[i]);
    }
}

Input with Leading zeros

Only thing I am unsure about your requirements is leading zeros (and perhaps you need to think about that for your own solution too).

  • Is 01234 a valid 5 digits input ?

Ignoring leading zeros

If leading zeros are to be ignored, then reading an int and converting it to String will remove the leading zeros.

Scanner input = new Scanner(System.in);
int number = input.nextInt();
String userInput = String.valueOf(number);
if (userInput.length() == 5) {
    String[] split = userInput.split("(?<=[0-9])");

    System.out.printf("Digits in %s are", userInput);
    for (int i = 0; i < split.length; i++) {
        System.out.printf(" %s", split[i]);
    }
}
alexbt
  • 16,415
  • 6
  • 78
  • 87