Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  john
Language: JAVA
Code:
import java.util.Scanner;
public class ISBN {

     public static void main(String[] args) {
          
          Scanner sc=new Scanner(System.in);
          int sum=0,i,flag=-1,len,pos=-1;
          String inp;
          char c;
          int []arr=new int[10];
          inp = sc.next();
          try{
               len=inp.length();
               
               if(len<10 || len>10)
                    throw new ArrayIndexOutOfBoundsException();
               for(i=0;i<len;i++){
                    c=inp.charAt(i);
                    if((c>='0') && (c<='9')){
                         switch(c){
                              case '0':arr[i]=0;break;
                              case '1':arr[i]=1;break;
                              case '2':arr[i]=2;break;
                              case '3':arr[i]=3;break;
                              case '4':arr[i]=4;break;
                              case '5':arr[i]=5;break;
                              case '6':arr[i]=6;break;
                              case '7':arr[i]=7;break;
                              case '8':arr[i]=8;break;
                              case '9':arr[i]=9;break;
                         }
                    }
                    else{
                         if(c=='X' && i==9){
                              arr[i]=10;
                         }
                         else{
                              if(c=='?' && flag ==-1){
                                   pos=i;
                                   flag=0;
                              }
                              else{
                                   flag=-2;
                                   break;
                              }
                         }
                    }
               }
               
               if(flag!=-2 && pos!=-1){

                    for(i=0;i<pos;i++){
                         sum+=arr[i]*(10-i);
                    }
               
                    for(i+=1;i<10;i++){
                         sum+=arr[i]*(10-i);
                    }
               
                    i=-1;
                    flag=0;
                    while(((sum+(++i*(10-pos))))%11!=0){
                         if(i>10){
                              flag=1;
                              System.out.println("NO SOLUTION POSSIBLE");
                              break;
                         }
                    }
                    
                    if(flag==0)
                         if(i==10)
                              System.out.println("X");
                         else{
                              System.out.println(i);
                         }
               }
               else{
                    System.out.println("Invalid Input");
               }
          
          }
          catch(ArrayIndexOutOfBoundsException e){
               System.out.println("Invalid Input");
          }
     }
}
          
Comments: