Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Sandbox {
- public static void main(String args[]) {
- try {
- Scanner sc = new Scanner(System.in);
- System.out.print("Please enter 9 symbols (digits): ");
- String str = sc.nextLine();
- //String str = "111222333";
- if (str.length() == 9){
- boolean hasDigits = true;
- for(int i = 0; i < 9; ++i){
- hasDigits = hasDigits && Character.isDigit(str.charAt(i));
- }
- if(hasDigits){
- int[] c = new int[10];
- for(int i = 0; i < 9; ++i){
- ++c[str.charAt(i)-'0'];
- }
- boolean isUnique = true;
- for(int i = 1; i < 10; ++i){
- isUnique = isUnique && (c[i] == 1);
- if(c[i] > 1){
- System.out.println("Value " + i + " shows up " + c[i] + " times");
- }
- }
- if (isUnique){
- System.out.println("All unique numbers");
- } else {
- System.out.println("Duplicates found");
- }
- } else {
- System.out.println("Invalid characters, must be digits");
- }
- } else {
- System.out.println("Wrong length, must be 9");
- }
- sc.close();
- } catch(Exception e){
- System.out.println(e);
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement