import java.util.*;
import java.util.Random;
import java.lang.*;
import java.util.Scanner;
public class testing1
{
    public static void main(String[] args)
    {
        String pass = new String("ENTER THE HAMLET HAM TO BE OR NOT TO BE THAT IS THE QUESTION WHETHER TIS NOBLER IN THE MIND TO SUFFER THE SLINGS AND ARROWS OF OUTRAGEOUS FORTUNE OR TO TAKE ARMS AGAINST A SEA OF TROUBLES AND BY OPPO SING END");
        
/////////////CONVERTING THE STRINGS INTO ASCII CODE//////////////////////////////////////////////////////////////////
        
        int[] passnum = new int[pass.length()];
        
        for(int i=0; i<pass.length(); i++)
        {
            if(pass.charAt(i)>64 && pass.charAt(i)<91)
            {
                passnum[i] = pass.charAt(i)-65;
            }
            else
            {
                passnum[i] = 26;
            }
        }
        
////////////////////////////////////////Encoding///////////////////////////////////////////////////////////////////////////////////////////
        int[] g = new int[27];
        
        Random random = new Random();
        
        for(int i=0; i<g.length; i++)
        {
            g[i] = i;
           // System.out.println(g[i]);
        }
        
        for(int i=0; i<50; i++)
        {
            int e = random.nextInt(26);
            int f = random.nextInt(26);
            
            int temp = g[e];
            g[e] = g[f];
            g[f] = temp;
            
        }
        /*
        for(int i=0; i<g.length; i++)
        {
            System.out.println(g[i]);
        }*/
        
        int[] encoded = new int[passnum.length];
        
        for(int i=0; i<passnum.length; i++)
        {
            encoded[i] = g[passnum[i]];
        }
        /* 
        for(int i=0; i<encoded.length; i++)
        {
           System.out.println(encoded[i]);
        }*/
       
        for(int i=0; i<encoded.length; i++)
        {
            char character =  (char) (encoded[i] + 65);
            //System.out.println(character);
        }
        
////////////////CREATING A TABLE/////////////////////////////////////////////////////////////////////////////////////////

        int table[][] = new int [27][27];
        
        for(int row=0; row<table.length; row++)
        {
            for(int column=0; column<table[row].length; column++)
            {
                table[row][column] = 0;
            }
        }
        
//////////////////////FILLING A TABLE//////////////////////////////////////////////////////////////////////////////////////    
    
        for(int i=0; i<passnum.length-2; i++)
        {
            table[passnum[i]][passnum[i+1]]++;
        }
        /*
        for(int row=0; row<table.length; row++)
        {
            for(int column=0; column<table[row].length; column++)
            {
                System.out.print(table[row][column]+"\t");
            }
            
            System.out.println();
        }*/
//////////////////////// PLAUSIBILITY ////////////////////////////////////////////////////////////////////////////////////////      
      
        int[] f = new int[27];
        
        for(int i=0; i<f.length; i++)
        {   
            f[i] = i;
            //System.out.println(f[i] + "\n");
        }

        int[] fstar = new int[27];
        
        for(int i=0; i<fstar.length; i++)
        {
            fstar[i] = f[i];
            //System.out.println(f[i] + "\n");
        }
    
        for(int s=0; s<2000; s++)
        {
            Random r = new Random();
            int n = 26;
            int temp;
                  
            int i=r.nextInt(n+1);
            int j= r.nextInt(n+1);
            
            temp = fstar[i];
            fstar[i] = fstar[j];
            fstar[j] = temp;
            
            int plf = 1;
            int plfstar = 1;
            
            for(int m=0; m<encoded.length-2; m++)
            {
                plf = plf* table[f[encoded[m]]][f[encoded[m+1]]];
                plfstar = plfstar*table[fstar[encoded[m]]][fstar[encoded[m+1]]];
            }
                
            if(plfstar > plf)
            {
                for(int a=0; a<27; a++)
                {
                    f[i] = fstar[i];
                }
            }
            else
            {
                Random rand = new Random();
                double coin = rand.nextDouble();
                
                if(coin <= plfstar/plf) 
                {
                    for(int b=0; b<27; b++)
                    {
                        f[i] = fstar[i];
                    }
                }
                else
                {
                    for(int c=0; c<27; c++)
                    {
                        f[i] = f[i];
                    }
                }
            }
        }
        
        for(int i=0; i<encoded.length; i++)
        {
            char character =  (char)(fstar[encoded[i]] + 65);
            System.out.println(character);
        }
    }
}

The above program is giving me an error that I need to solve. The error is on this line " if (coin <= plfstar/plf)" because the value is of them is zero but I do not know why their value is zero where it should be between 0 and 1.

kindly print the value of the the divided statement.

System.out.println( plfstar/plf);

And check whether what output you are receiving.

I am getting the same java lang arithmetic exception divide by zero.

Look at your line 136

plf = plf* table[f[encoded[m]]][f[encoded[m+1]]];

The 'plf' becomes 0 when the value of 'table[f[encoded[m]]][f[encoded[m+1]]]' is equal to 0. The error comes from this line because you do not check what values inside the 'table' array is before you multiply with 'plf'. As a result, the plf value is 0 and cause the divided by zero exception.

So what should I do then as I am not good at java..... Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.