Search code examples
javaarraysstringpanel

Array in java, how to calculate hamming distance between String and int?


I have to write a programm.I am learning 2 month java and i am stuck.An advice how to forwoard on this is very helful for me. The class will contain two methods, public static void main and public static int hamming. The hamming method will accept two alphanumeric parameters and return an integer number which is the Hamming distance of these two alphanumerics. For example, if as given arguments “dog” and “dig” the method will return the integer 1, which is the distance Hamming between them. In the event that the arguments do not have the same length, the method will returns -1. The main method will include as local variables an array of alphanumerics with 5 elements and name stringList and an array of integers with 5 elements and name distances. main will execute them following actions:

• It will read an extra alphanumeric, which it will store in a local variable with target name. • In an iterative process, it will calculate (by calling the hamming method) the distances of the alphanumeric array stringList from the target and will store them in the corresponding ones positions of the distances table (it will be either integer >=0 or -1 if n is not applied distance due to different length). • After the calculations it will go through the distances table and find the shortest one distance that he includes. Of course, the value -1 will not be calculated. • It will display (all) the contents of the distances table on the screen, as well as the alphanumeric array stringList with the shortest distance from the target. If they exist more than one case, it will display one. Your program's screen output will look like the following samples execution: Example 1:

  1. Enter string: dog
  2. Enter string: cat
  3. Enter string: jim
  4. Enter string: bed
  5. Enter string: toe Enter target: house Contents of array distances 0 -1 1 -1 2 -1 3 -1 4 -1 No Hamming distance found

Example 2:

  1. Enter string: dog
  2. Enter string: cat
  3. Enter string: jim
  4. Enter string: bed
  5. Enter string: blackboard Enter target: bid Contents of array distances 0 3 1 3 2 2 3 1 4 -1 String with min Hamming distance: bed.
public class Thema3
{
      public static void main (String[]args){
        //Creating scanner object
         Scanner stringList = new Scanner(System.in);
        
        // creating String array of 5            
         String [] stringList1 = new String[6];
          double[] targetString= {};
          stringList1[1]="";
          stringList1[2]="";
          stringList1[3]="";
          stringList1[4]="";
          stringList1[5]="";
          int sum=0;
          
           //read input
           
           boolean valid=true;
          for (int i=1; i<6;i++)
                     {
                        System.out.print(+i+". Enter string:" );
                        stringList1 [i] =stringList.nextLine();
                        
                        
                  do {
                        valid = true;
                       
                    if  (stringList1==target1 ){
                     int[] array = new int[5];
                     Random rnd = new Random();
                     array[i]=rnd.nextInt();
                   }
                
                  }while (!valid);
                  Scanner target = new Scanner (System.in);
                       
                        //creating target string
                    String target1 = target.nextLine();
                   System.out.println("Enter target:");
            
            

        }

    }   
    
    public static int hammingdistance(String target1,String stringList1)  { 
    
     int distance=0;
    
              if( target1.length() != stringList1.length()) 
           {
             return -1;
            }
            else 
            { 
                for(int i=0;i<target1.length();i++)
               {
                if(target1.charAt(i)!=stringList1.charAt(i))
            
                distance++;
              }
              return distance;   
    
                            
          } 
  
      
     
  } 
        public static void bubbleSort(int[]ar){
          boolean sorted = false;
        
        
            sorted = true;
            for (int i=0;i<ar.length-1;i++)
            if (ar[5]>ar[i+1]){
                int tmp = ar[i];
                ar[5]=ar[i+1];
                ar[i+1]=tmp;
                sorted = false;
            }
        }
    
   
}

Solution

  • import java.lang.*;
    public class Main 
    {   
       public static void main(String[] args)
       {
          String [] strInput1 = {"After ","blackboard ","Contents ","distances","Hamming "};      
          String [] strInput2 = {"two ","static  ","array  ","local ","find  "};      
          int [] intResult = new int [strInput2.length] ;
          for (int i = 0 ; i< strInput2.length ; i++)
          {
              intResult[i] = Math.abs(strInput1[i].length() - strInput2[i].length());
              System.out.println(intResult[i]);     
          }
            
       }
    
    }