Advertisement
irmantas_radavicius

Untitled

Mar 2nd, 2022
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. import java.util.*;
  2.  
  3.  
  4. public class Sandbox {
  5.  
  6.     public static void main(String args[]) {       
  7.         try {      
  8.             Scanner sc = new Scanner(System.in);
  9.  
  10.             System.out.print("Please enter 9 symbols (digits): ");
  11.             String str = sc.nextLine();
  12.             //String str = "111222333";
  13.            
  14.             if (str.length() == 9){    
  15.                 boolean hasDigits = true;
  16.                 for(int i = 0; i < 9; ++i){
  17.                     hasDigits = hasDigits && Character.isDigit(str.charAt(i));
  18.                 }  
  19.                 if(hasDigits){                 
  20.                     int[] c = new int[10];
  21.                     for(int i = 0; i < 9; ++i){
  22.                         ++c[str.charAt(i)-'0'];
  23.                     }  
  24.                     boolean isUnique = true;               
  25.                     for(int i = 1; i < 10; ++i){
  26.                         isUnique = isUnique && (c[i] == 1);
  27.                         if(c[i] > 1){
  28.                             System.out.println("Value " + i + " shows up " + c[i] + " times");
  29.                         }
  30.                     }                              
  31.                     if (isUnique){
  32.                         System.out.println("All unique numbers");
  33.                     } else {
  34.                         System.out.println("Duplicates found");
  35.                     }      
  36.                 } else {
  37.                     System.out.println("Invalid characters, must be digits");
  38.                 }              
  39.             } else {
  40.                 System.out.println("Wrong length, must be 9");
  41.             }
  42.            
  43.             sc.close();        
  44.  
  45.         } catch(Exception e){
  46.             System.out.println(e);
  47.             System.out.println("Unexpected error, sorry!");
  48.         }      
  49.        
  50.     }
  51.    
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement