I am getting an array out of index error 26 pls I really need help.

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 [26][26];

		
		for(int row=0; row<table.length; row++){
		for(int column=0; column<table[row].length; column++){
			table[row][column] = 0;
		System.out.print(table[row][column]+"\t");
		}
		System.out.println();
		}
		
	
		for(int i=0; i<passnum.length-2; i++){
			
			table[passnum[i]][passnum[i++]]++;}

Recommended Answers

All 5 Replies

passnum = 26;}

int table[][] = new int [26][26];

table[passnum][passnum[i++]]++

Now can you see it? (the max valid index for an array[26] is 25)

I fix that error now I am getting the other similar one

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];}
				}
			}
		}

The error message tells you which line it happened on. So which was it?
ps next time post your code in CODE tags.

the error is on this line plf = plf * table[f[passnum[m]]][f[passnum[m+1]]];}

On the line before that put print statements to show you exactly what values there are for passnum[m], f[passnum[m]] etc so you can see which one is going out of range and when.

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.