Home > front end >  Program is running but when I enter 9 digit error starting with 0, its giving me error that its inva
Program is running but when I enter 9 digit error starting with 0, its giving me error that its inva

Time:10-08

The program is running but when I enter 9 digit error starting with 0, it's giving me an error that its invalid but I want print it as not used character. Its java basic learning, thanks for helping in advance. My whole code is running perfect and if I enter 9 digit starting with 8 then its working properly for 0, its giving me error.

 import java.util.Scanner;

 public class FirstDigitSIN
 {
 public static void main(String[] args)
 {
 
 Scanner kb = new Scanner(System.in);
 System.out.print("Enter a SIN number :" );
if(kb.hasNextInt()){
// code that you want executed if the input is an integer goes in here 
int num = kb.nextInt();
 int numCopy = num;
 int digit =0;
     int count = 0;
     while(num != 0 ){
         num = num/10;
         count  ;}
     if(count != 9){
        System.out.println("Invalid Entry"); }
         else{
         System.out.println( num );
        
     

 while(numCopy>0){
     digit = numCopy %10;
     
     numCopy = numCopy/10;
 }if (digit == 1){
     System.out.println(" Registered in Atlantic Canada ");
 }else if(digit == 2){
     System.out.println(" Registered in Quebec ") ;
 }else if (digit == 3){
     System.out.println(" Registered in Quebec ");}
  else   if (digit == 4){
     System.out.println(" Registered in Ontario ");}
   else  if (digit == 5){
     System.out.println(" Registered in Ontario ");}
   else  if (digit == 6){
     System.out.println(" Registered in Prairie Provinces, NWT, Nunavut, Northwest Ontario ");}
  else   if (digit == 7){
     System.out.println(" Registered in Pacific region ");}
  else   if (digit == 9){
     System.out.println(" A temporary SIN ");}
     else if (digit ==8 && digit ==0) {
         System.out.println( "Not used");
     }}
     System.out.println(" *** End of program *** ");
    } 
    else 
    {
    System.out.println ("Error message goes here. Here you can tell them that you want them to       enter an integer and not a string.");
    }

    }
    }

CodePudding user response:

Your problem is that Java is treating your Integer as 8 integers long. This is causing it to not be a valid input.

I made an if statement to check if it is treated as 8 characters long and if its 8 long and the first character is a 0 it prints not used since phone numbers don't start with 0.

if(count != 9 && (String.valueOf(Math.abs((long)num)).charAt(0) == '0')){
                System.out.println( "Not used");

            }

Full Code

import java.util.Scanner;

public class FirstDigitSIN
{
    public static void main(String[] args)
    {

        Scanner kb = new Scanner(System.in);
        System.out.print("Enter a SIN number :" );
        if(kb.hasNextInt()){
            // code that you want executed if the input is an integer goes in here 
            int num = kb.nextInt();
            int numCopy = num;
            int digit =0;
            int count = 0;
            while(num != 0 ){
                num = num/10;
                count  ;}
            if(count != 9 && (String.valueOf(Math.abs((long)num)).charAt(0) == '0')){
                System.out.println( "Not used");

            }
            else    if(count != 9){
                System.out.println("Invalid Entry"); }

            else{
                System.out.println( num );

                while(numCopy>0){
                    digit = numCopy %10;

                    numCopy = numCopy/10;
                }if (digit == 1){
                    System.out.println(" Registered in Atlantic Canada ");
                }else if(digit == 2){
                    System.out.println(" Registered in Quebec ") ;
                }else if (digit == 3){
                    System.out.println(" Registered in Quebec ");}
                else   if (digit == 4){
                    System.out.println(" Registered in Ontario ");}
                else  if (digit == 5){
                    System.out.println(" Registered in Ontario ");}
                else  if (digit == 6){
                    System.out.println(" Registered in Prairie Provinces, NWT, Nunavut, Northwest Ontario ");}
                else   if (digit == 7){
                    System.out.println(" Registered in Pacific region ");}
                else   if (digit == 9){
                    System.out.println(" A temporary SIN ");}
                else if (digit ==8 && digit ==0) {
                    System.out.println( "Not used");
                }}
            System.out.println(" *** End of program *** ");
        } 
        else 
        {
            System.out.println ("Error message goes here. Here you can tell them that you want them to       enter an integer and not a string.");
        }

    }
}

CodePudding user response:

Couple of things:

  • To Avoid skipping these leading 0 use String rather than int to hold the input.
  • Then use Regex pattern to validate that String contains only numbers (else throw error if not a number).
  • As in your code you want to check the value of first digit in number entered by user, to retrieve that get first index from entered String.
  • The last condition digit == 8 && digit == 0 should be replaced with digit == 8 || digit == 0, as here we are checking equality of single value. And single value can't be equal to both 8 and 0.

Here is the code:

public class FirstDigitSIN {
    private static final String NUMBERS = "[0-9] ";
    
      // Compile the ReGex
      private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBERS);
    
      public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
        Scanner kb = new Scanner(System.in);
        System.out.print("Enter a SIN number :");
        if (kb.hasNext()) {
          // code that you want executed if the input is an integer goes in here 
          String num = kb.next();
    
          // using Pattern.matcher()
          Matcher m = NUMBER_PATTERN.matcher(num);
    
          // Return if the string
          // matched the ReGex
          if (m.matches()) {
            int numCopy = Integer.valueOf(num);
            int digit = 0;
            if (num.length() != 9) {
              System.out.println("Invalid Entry");
            } else {
              System.out.println(num);
              int digit = Character.getNumericValue(num.charAt(0));
              System.out.println(digit);

              if (digit == 1) {
                System.out.println(" Registered in Atlantic Canada ");
              } else if (digit == 2) {
                System.out.println(" Registered in Quebec ");
              } else if (digit == 3) {
                System.out.println(" Registered in Quebec ");
              } else if (digit == 4) {
                System.out.println(" Registered in Ontario ");
              } else if (digit == 5) {
                System.out.println(" Registered in Ontario ");
              } else if (digit == 6) {
                System.out.println(" Registered in Prairie Provinces, NWT, Nunavut, Northwest Ontario ");

              } else if (digit == 7) {
                System.out.println(" Registered in Pacific region ");
              } else if (digit == 9) {
                System.out.println(" A temporary SIN ");
              } else if (digit == 8 || digit == 0) {
                System.out.println("Not used");
              }
            }
            System.out.println(" *** End of program *** ");
          } else {
            System.out.println(
                "Error message goes here. Here you can tell them that you want them to       enter an integer and not a string.");
          }
    
        } else {
          System.out.println(
              "Error message goes here. Here you can tell them that you want them to       enter an integer and not a string.");
        }
      }
}
  •  Tags:  
  • java
  • Related