Ihave written the below pieces of code, but i am not able to solve the compilation error here as it shows the error as ArrayOutOfBoundsException error. so please tell me hw to remove the error and please bring the necessary changes.

import java.util.*;

public class Cipher
{
	private int n;
	private int p;
	private int q;
	private int d;
	private int phi;
	private int e;
	public void generatePQ()
	{
		int pickedNumber1 = 0;
		int pickedNumber2 = 0;
		boolean b = false;
		Random rand = new Random(); 
		while(true)
		{
			pickedNumber1 = rand.nextInt(40)+2; 
			b = isPrime(pickedNumber1);
			if(b == true)
			{
				break;
			}
		}
		setP(pickedNumber1);
		while(true)
		{
			pickedNumber2 = rand.nextInt(40)+2; 
			b = isPrime(pickedNumber2);
			if(b == true)
			{
				break;
			}
		}	
		setQ(pickedNumber2);
	}	
	public static boolean isPrime(int n) 
	{
		for(int i=2;i<n;i++) 
		{
			if(n%i==0)
				return false;
		}	
		return true;
	}
	
	public void setP(int a)
	{
		p = a;
	}
	public void setQ(int b)
	{
		q = b;
	}
	public int getP()
	{
		return p;
	}
	public int getQ()
	{
		return q;
	}
	
	public void generateNandPhi()
	{
		int p = getP();
		int q = getQ();
		n = p * q;
		phi = (p-1) * (q-1);
		setN(n);
		setPhi(phi);
	}
	public void setN(int a)
	{
		n = a;
	}
	public void setPhi(int b)
	{
		phi = b;
	}
	public int getN()
	{
		return n;
	}
	public int getPhi()
	{
		return phi;
	}
	
	public int generateEandD()
	{
		int e1 = 0;
		int p_numbers[] = {2,5,7,11,13,17,19,23,29,31,37,41};
		int phi = getPhi();
		int n = getN(); 		
		for(int i = 0; i < 12;i++)
		{
			if(((phi%p_numbers[i] ) != 0) && (p_numbers[i] < phi))
			{
				e1 = p_numbers[i];
				setE(e1);
				break;
			}
		}
		int vals[] = gcd(phi, e1);
		try
		{
			int temp = phi + vals[0];
			int d1 = temp / e1;
			setD(d1);
		}
		catch(ArithmeticException ex)
		{
			System.out.println("Arithmetic Exception occurs");
		}
		return e1;
	}
	
	public void setE(int a)
	{
		e = a;
	}
	public int getE()
	{
		return e;
	}
	public void setD(int a)
	{
		d = a;
	}
	public int getD()
	{
		return d;
	}
	public int[] StringToInt(String pt)
	{
		int j =0;
		int k[] = new int[pt.length()];
		for(int i=0;i<pt.length();++i)
		{
			char c = pt.charAt( i );
			j = (int) c;
			k[i] = j;
		}
		return k;
	}
	public int getCipherText(int[] c, int e)
	{
		int cipher = 0;
		try
		{
			cipher = (int)((Math.pow(c[c.length-1],e)) % n);
		}
		catch(ArrayIndexOutOfBoundsException ex)
		{
			System.out.println("Array out of Bounds Exception occured");
		}
		return cipher;
	}
	public int[] gcd(int m, int n) 
	{
		if (n == 0)
			return new int[] { m, 1, 0 };
		int[] vals = gcd(n, m % n);
		int d = vals[0];
		int a = vals[2];
		int b = vals[1] - (m / n) * vals[2];
		return new int[] { d, a, b };
	}
	public String IntToString(int cipherInt) 
	{
		String cipherString = Integer.toString(cipherInt);
		return cipherString;
    }
}
import javax.swing.*;

public class CipherInterface extends JFrame
{
	public CipherInterface()
	{
		setTitle("Cipher");
		setBounds(200,200,300,200);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new CipherPanel();
		this.add(panel);
	}
}
import javax.swing.*;

public class CipherInterfaceTest
{
	public static void main(String ar[])
	{
		JFrame f = new CipherInterface();
		f.setVisible(true);
		Cipher r = new Cipher();
		r.generatePQ();
		r.generateNandPhi();
		int er = r.generateEandD();
		CipherPanel p = new CipherPanel();
		String plaintext = p.getS();
		int m[] = r.StringToInt(plaintext);
		int l = m.length;
		//System.out.print("length = " +l);
		int ct = r.getCipherText(m,er);
		String sct = r.IntToString(ct);
		p.setCipher(sct);
	}
}
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CipherPanel extends JPanel
{
	private JTextField enterdata, ciphertext;
	private JLabel datalabel, ciphertextlabel;
	private JButton sendbutton, cancelbutton;
	private String s = new String();
	
	public CipherPanel()
	{
		// displa panel
		JPanel displaypanel = new JPanel();
		displaypanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		// data label
		datalabel = new JLabel("Enter the message: ");
		displaypanel.add(datalabel);
		// enter data text field
		enterdata = new JTextField(10);
		enterdata.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				setS(enterdata.getText());
				System.out.print("String value =: "+s);
			}
		});
		displaypanel.add(enterdata);
		// cipher text label
		ciphertextlabel = new JLabel("Cipher Text generated: ");
		displaypanel.add(ciphertextlabel);
		// ciphertext text field
		ciphertext = new JTextField(10);
		ciphertext.setEditable(false);
		ciphertext.setFocusable(false);
		displaypanel.add(ciphertext);
		
		//button panel
		JPanel buttonpanel = new JPanel();
		buttonpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		// send button
		sendbutton = new JButton("Send");
		buttonpanel.add(sendbutton);
		// cancel button
		cancelbutton = new JButton("Cancel");
		buttonpanel.add(cancelbutton);
		
		// add panels to the main panel
		this.setLayout(new BorderLayout());
		this.add(displaypanel,BorderLayout.CENTER);
		this.add(buttonpanel,BorderLayout.SOUTH);
	}
	public void setS(String txt)
	{
		s = txt;
	}
	public String getS()
	{
		System.out.println(s);
		return s;
	}
	
	public void setCipher(String panelCipher)
	{
		ciphertext.setText(panelCipher);
	}
}

please help me out?

Ihave written the below pieces of code, but i am not able to solve the compilation error here as it shows the error as ArrayOutOfBoundsException error. so please tell me hw to remove the error and please bring the necessary changes.

import java.util.*;

public class Cipher
{
	private int n;
	private int p;
	private int q;
	private int d;
	private int phi;
	private int e;
	public void generatePQ()
	{
		int pickedNumber1 = 0;
		int pickedNumber2 = 0;
		boolean b = false;
		Random rand = new Random(); 
		while(true)
		{
			pickedNumber1 = rand.nextInt(40)+2; 
			b = isPrime(pickedNumber1);
			if(b == true)
			{
				break;
			}
		}
		setP(pickedNumber1);
		while(true)
		{
			pickedNumber2 = rand.nextInt(40)+2; 
			b = isPrime(pickedNumber2);
			if(b == true)
			{
				break;
			}
		}	
		setQ(pickedNumber2);
	}	
	public static boolean isPrime(int n) 
	{
		for(int i=2;i<n;i++) 
		{
			if(n%i==0)
				return false;
		}	
		return true;
	}
	
	public void setP(int a)
	{
		p = a;
	}
	public void setQ(int b)
	{
		q = b;
	}
	public int getP()
	{
		return p;
	}
	public int getQ()
	{
		return q;
	}
	
	public void generateNandPhi()
	{
		int p = getP();
		int q = getQ();
		n = p * q;
		phi = (p-1) * (q-1);
		setN(n);
		setPhi(phi);
	}
	public void setN(int a)
	{
		n = a;
	}
	public void setPhi(int b)
	{
		phi = b;
	}
	public int getN()
	{
		return n;
	}
	public int getPhi()
	{
		return phi;
	}
	
	public int generateEandD()
	{
		int e1 = 0;
		int p_numbers[] = {2,5,7,11,13,17,19,23,29,31,37,41};
		int phi = getPhi();
		int n = getN(); 		
		for(int i = 0; i < 12;i++)
		{
			if(((phi%p_numbers[i] ) != 0) && (p_numbers[i] < phi))
			{
				e1 = p_numbers[i];
				setE(e1);
				break;
			}
		}
		int vals[] = gcd(phi, e1);
		try
		{
			int temp = phi + vals[0];
			int d1 = temp / e1;
			setD(d1);
		}
		catch(ArithmeticException ex)
		{
			System.out.println("Arithmetic Exception occurs");
		}
		return e1;
	}
	
	public void setE(int a)
	{
		e = a;
	}
	public int getE()
	{
		return e;
	}
	public void setD(int a)
	{
		d = a;
	}
	public int getD()
	{
		return d;
	}
	public int[] StringToInt(String pt)
	{
		int j =0;
		int k[] = new int[pt.length()];
		for(int i=0;i<pt.length();++i)
		{
			char c = pt.charAt( i );
			j = (int) c;
			k[i] = j;
		}
		return k;
	}
	public int getCipherText(int[] c, int e)
	{
		int cipher = 0;
		try
		{
			cipher = (int)((Math.pow(c[c.length-1],e)) % n);
		}
		catch(ArrayIndexOutOfBoundsException ex)
		{
			System.out.println("Array out of Bounds Exception occured");
		}
		return cipher;
	}
	public int[] gcd(int m, int n) 
	{
		if (n == 0)
			return new int[] { m, 1, 0 };
		int[] vals = gcd(n, m % n);
		int d = vals[0];
		int a = vals[2];
		int b = vals[1] - (m / n) * vals[2];
		return new int[] { d, a, b };
	}
	public String IntToString(int cipherInt) 
	{
		String cipherString = Integer.toString(cipherInt);
		return cipherString;
    }
}
import javax.swing.*;

public class CipherInterface extends JFrame
{
	public CipherInterface()
	{
		setTitle("Cipher");
		setBounds(200,200,300,200);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new CipherPanel();
		this.add(panel);
	}
}
import javax.swing.*;

public class CipherInterfaceTest
{
	public static void main(String ar[])
	{
		JFrame f = new CipherInterface();
		f.setVisible(true);
		Cipher r = new Cipher();
		r.generatePQ();
		r.generateNandPhi();
		int er = r.generateEandD();
		CipherPanel p = new CipherPanel();
		String plaintext = p.getS();
		int m[] = r.StringToInt(plaintext);
		int l = m.length;
		//System.out.print("length = " +l);
		int ct = r.getCipherText(m,er);
		String sct = r.IntToString(ct);
		p.setCipher(sct);
	}
}
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CipherPanel extends JPanel
{
	private JTextField enterdata, ciphertext;
	private JLabel datalabel, ciphertextlabel;
	private JButton sendbutton, cancelbutton;
	private String s = new String();
	
	public CipherPanel()
	{
		// displa panel
		JPanel displaypanel = new JPanel();
		displaypanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		// data label
		datalabel = new JLabel("Enter the message: ");
		displaypanel.add(datalabel);
		// enter data text field
		enterdata = new JTextField(10);
		enterdata.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				setS(enterdata.getText());
				System.out.print("String value =: "+s);
			}
		});
		displaypanel.add(enterdata);
		// cipher text label
		ciphertextlabel = new JLabel("Cipher Text generated: ");
		displaypanel.add(ciphertextlabel);
		// ciphertext text field
		ciphertext = new JTextField(10);
		ciphertext.setEditable(false);
		ciphertext.setFocusable(false);
		displaypanel.add(ciphertext);
		
		//button panel
		JPanel buttonpanel = new JPanel();
		buttonpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		// send button
		sendbutton = new JButton("Send");
		buttonpanel.add(sendbutton);
		// cancel button
		cancelbutton = new JButton("Cancel");
		buttonpanel.add(cancelbutton);
		
		// add panels to the main panel
		this.setLayout(new BorderLayout());
		this.add(displaypanel,BorderLayout.CENTER);
		this.add(buttonpanel,BorderLayout.SOUTH);
	}
	public void setS(String txt)
	{
		s = txt;
	}
	public String getS()
	{
		System.out.println(s);
		return s;
	}
	
	public void setCipher(String panelCipher)
	{
		ciphertext.setText(panelCipher);
	}
}

please help me out?

This thread seems to me really much the same as here:http://www.daniweb.com/software-development/java/threads/411711 except now you've posted all your code. please try keep it all in one thread as this pertains to the same question you asked on that thread on the same code. Or at least mark your previous thread as solved and perhaps cite the new question and point to the new thread. i.e this thread...

Edited 4 Years Ago by DavidKroukamp: n/a

which one? and don't give a description, post the error message here, since it will tell us exactly where the error occurs.
that'll save us the time to read all that code above.
to give you a bit of an idea of what your problem is:
if you have an array containing two elements, there are two valid indexes you can use: 0 (the first element) and 1 (the second and last element).
if you are iterating over your array and pass these bounds, for instance, you ask:
element = myArray[2]; // here you're trying to get the third element, which
// doesn't exist
you'll get that error.

the problem is that when i am compiling the program, there occurs errors like Number formatException, etc. The problem is that the CipherInterfaceTest.java doesn't take input and pass it to its designated functions?? i think anyone who wants to help me should try to run my code first.

the problem is that when i am compiling the program, there occurs errors like Number formatException, etc. The problem is that the CipherInterfaceTest.java doesn't take input and pass it to its designated functions?? i think anyone who wants to help me should try to run my code first.

yes, our day evolves around solving your problem.
you're creating double threads and don't even bother to show us what exception and error message you are getting. we are volunteers, who solve the problems here next to our own jobs and personal life.

I think anyone who wants to help you can expect you to actually provide them with the info they need, but hey, that's just me.

i have been woring on it whole day, but now my compilation error is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CipherInterfaceTest.main()

which is not the complete error message. I'm not asking a summary of the error, but the entire message, which also states on which line the error occurs.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CipherInterfaceTest.main(CipherInterfaceTest.java:18)

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CipherInterfaceTest.main(CipherInterfaceTest.java:18)

just looking at your code:

int er = r.generateEandD();
		CipherPanel p = new CipherPanel();
		String plaintext = p.getS();
		int m[] = r.StringToInt(plaintext);
		int l = m.length;
		//System.out.print("length = " +l);
		int ct = r.getCipherText(m,er);
		String sct = r.IntToString(ct);
		p.setCipher(sct);

shouldnt you be passing r.getCipherText(m,l); because look at your method generateEandD() from where you get the value 'er' and pass it to getCipherText(m,er):

public int generateEandD()
	{
		int e1 = 0;
		int p_numbers[] = {2,5,7,11,13,17,19,23,29,31,37,41};
		int phi = getPhi();
		int n = getN(); 		
		for(int i = 0; i < 12;i++)
		{
			if(((phi%p_numbers[i] ) != 0) && (p_numbers[i] < phi))
			{
				e1 = p_numbers[i];
				setE(e1);
				break;
			}
		}
		int vals[] = gcd(phi, e1);
		try
		{
			int temp = phi + vals[0];
			int d1 = temp / e1;
			setD(d1);
		}
		catch(ArithmeticException ex)
		{
			System.out.println("Arithmetic Exception occurs");
		}
		return e1;
	}

where is there a correlation/relationship between your arrays length and the value this method returns?

Edited 4 Years Ago by DavidKroukamp: n/a

i could understand what is the problem actually, but it is with the line 18 of class CipherInterfaceTest.

i could understand what is the problem actually, but it is with the line 18 of class CipherInterfaceTest.

yes i do know what line it occured on, and i wasnt explaining the problem i gave you what i thought was the knowledge to know which part of your codes logic is causing it to throw an error(and if you have a look i have taken code from what in your first post on this thread was the CipherInterfaceTest-and including line 18: int ct = r.getCipherText(m,er); ). I have failed in this... maybe someone with a better way to explain may be in need..

Edited 4 Years Ago by DavidKroukamp: n/a

i have changed the method StringToInt with the following one

public int StringToInt(String pt) 
	{
		int ptInt = 0;
		ptInt = Integer.parseInt(pt.trim());
		return ptInt;
	}

and also modified other methods for type compatibility, but now the error is:

Exception in thread "main" java.lang.NumberFormatException: For input string:""""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Cipher.StringToInt(Cipher.java:154)
at CipherInterfaceTest.main(CipherInterfaceTest.java:15)

i have changed the method StringToInt with the following one

public int StringToInt(String pt) 
	{
		int ptInt = 0;
		ptInt = Integer.parseInt(pt.trim());
		return ptInt;
	}

and also modified other methods for type compatibility, but now the error is:

Exception in thread "main" java.lang.NumberFormatException: For input string:""""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Cipher.StringToInt(Cipher.java:154)
at CipherInterfaceTest.main(CipherInterfaceTest.java:15)

well where are you initializing 'pt' which is the argument passed to the StringToInt()? because the exception says it all, the value of pt is "" and that cant be converted to an integer... but i'm confused as to why you changed that when that wasnt the problem that was giving you the ArrayOutOfBounds exception?

Edited 4 Years Ago by DavidKroukamp: n/a

i should get the value of pt from the CipherPanel that is entered by the user as input, but my program doesnt go to take input n that made me to change it because i think CipherInterface and CipherPanel are not working well to take input?

This question has already been answered. Start a new discussion instead.