import java.util.*;
import java.util.Random;
import java.lang.*;
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 OPPOSING END");

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;}
//System.out.println(passnum[i]);
}
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;
}
}


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();
}




int[] f = new int[27];
for(int i=0; i<f.length; i++){
f[i] = i;}

int[] fstar = new int[27];
for(int i=0; i<fstar.length; i++){
fstar[i] = f[i];}

for(int s=0; s<200; s++){
Random r = new Random();
int n = 27;
int temp;
int i=r.nextInt(n+1);
int j= r.nextInt(n+1);
temp = fstar[i];
fstar[i] = fstar[j];
fstar[j] = temp;

for(int k=0; k<passnum.length; k++){
int plf = passnum[k];
int plfstar = passnum[k];


for(int m=0; m<passnum.length-2; m++){
plf = plf * table[f[passnum[m]]][f[passnum[m+1]]];}


for(int l=0; n<passnum.length-2; l++){
plfstar = plfstar * table[f[passnum[l]]][f[passnum[l+1]]];}

if(plfstar > plf){
f[i] = fstar[i];break;}
else{
Random rand = new Random();
int c = 2;
int coin = rand.nextInt(c+1);

if(coin < (plfstar/plf)){
f[i] = fstar[i];}
else{
fstar[i] = f[i];}
}
}
}

Edited 6 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Help with what?

How to use code tags maybe? Select your code and click the button on the editor that says [code]

How to properly post a question? Use a descriptive title, not just "help" or "urgent" (trust me, it's not urgent to anyone else). Describe the problem. Post specific error messages and stack traces if you have them.

Appears to be something related with encoding... There is missing two parenthesis at the end. Some array exceeded the length. I did correct indentation with Eclipse, looks better now.

I have created an algorithm markoc chain to encrypt a piece of text. So want to know how to run that algorithm on text and display output in java

How do you want to get your text in? Command line or gui? File or keyboard input? Or maybe open a web connection and get text from some site? Or perhaps you want the user to tap the file in morse code on the machine's built-in microphone? I'm sure there's many other means of presenting some text to a program, so you'll have to pick at least one.

How do you want to display? Again, command line or gui is a good place to start.

The text will come from a file. The algorithm is top of this thread. The file will contain text like ESWQD RFDT FGT, so i need to run the above algorithm so after 2000 iteration it will print out the original text like enter from .....

Okay, now there's something to work with.
To read from a file, you need to get the name of the file - you want to get that from the user at the command line. Then you need to open the file and read in its contents. Then you need to call your algorithm method. Oh, wait, it's not a method.

Okay, there are two ways you could handle this. One is the obvious way: change the class above so the algorithm runs as a non-main method. Another, more devious way, would be to instantiate the above class and call its main() method - you'd have to change it to take its data from the String[] args array, and you'd have to pass in your data String (Strings? why not many Strings?) as an array. Plausible.

The option which you're probably considering is to simply continue to write one long toilet-paper main method on this class.
You can do that if you want, but you won't get me to help you fix it when it's broken - give me short methods that do simple things.


So I'm off to play some music. Here's your new main() method. You can write the new methods in a few hours, I think.

public static void main (String[] args)
{
  String fileName = getFileNameFromUser();
  String dataString = readFile(fileName);
  if (dataString == null)
    System.out.println("File read error on file name "+fileName);
  else 
    result = processData(dataString);

  System.out.println("Result: " + result);
}

There you go. I left you the easy part... :)

Notice that I didn't put a try block around the readFile() method - since this is a small local application, I didn't want to saddle you with exception handling, so I'm expecting you to eat any FileIOExceptions and return a null file if there's a problem there. If you'd rather, though, you can go ahead and toss them back and play with them in main, that's cool too.

Edited 6 Years Ago by jon.kiparsky: n/a

Give an outline of how one would create a class called AtomInt that would store an
integer which could be updated by threads without encountering race conditions using the
classes you have encountered so far. Initialising an AtomInt would store an initial value
(as a parameter). The class would have four methods
add which would add an integer value n to the stored value,
increment which would increment the stored value by one,
decrement the stored value by one;
show which would return the stored value.

Comments
Lame.
import java.util.*;
import java.util.Random;
import java.lang.*;
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 OPPOSING 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;
            }
        }



////////////////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 AN ARRAY//////////////////////////////////////////////////////////////////////////////////////    

        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<200; 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<passnum.length-2; m++)
            {

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

                plfstar = plfstar*table[fstar[passnum[m]]][fstar[passnum[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<passnum.length-1; i++)
        {

            char character =  (char)(f[passnum[i]] + 65);
            System.out.println(character);


        }




}




}

The above code when run give me an error java.Lang.Arithmetic Exception / zero. I do not know where the problem is. I think the error may be on this line (if coin<plfstar/plf)... If anybody can help...

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.