Hallo!
Weiß vllt jmd, wieso mein Programm mir einen Fehler bei encode(); und decode(); anzeigt?
Weiß vllt jmd, wieso mein Programm mir einen Fehler bei encode(); und decode(); anzeigt?
Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Vigenere extends JFrame {
private final String code;
private int count = 0;
private JTextField versTF = new JTextField();
@SuppressWarnings("rawtypes")
private JComboBox verentCB = new JComboBox();
@SuppressWarnings("rawtypes")
private JComboBox casvigCB = new JComboBox();
private JButton ConBT = new JButton();
private JLabel trenJL = new JLabel();
private JLabel keyJL = new JLabel();
private JLabel errJL = new JLabel();
private JTextArea InoutTA = new JTextArea("");
private JScrollPane InoutTAScrollPane = new JScrollPane(InoutTA);
String CBcv ="", TFkey = "" , input = "", output = "";
int numchar = 0, numcae = 0, numkey = 0, i = 0;
char[] inarray;
char [] keyarray;
boolean error = false, testnum = true, Decrypt = false;
@SuppressWarnings("unchecked")
public Vigenere(String title) {
super(title);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int frameWidth = 500;
int frameHeight = 350;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
cp.setBackground(Color.WHITE);
setTitle("The Vigenére-Machine");
versTF.setBounds(25, 160, 150, 50);
versTF.setForeground(Color.BLACK);
versTF.setFont(new Font("Dialog", Font.PLAIN, 16));
versTF.setHorizontalAlignment(SwingConstants.CENTER);
cp.add(versTF);
verentCB.setBounds(25, 70, 150, 30);
verentCB.setBackground(Color.WHITE);
verentCB.addItem("Encode text");
verentCB.addItem("Decode text");
cp.add(verentCB);
casvigCB.setBounds(25, 10, 150, 30);
casvigCB.setBackground(Color.WHITE);
casvigCB.addItem("Caesar");
casvigCB.addItem("Vigenère");
cp.add(casvigCB);
ConBT.setBounds(25, 265, 150, 50);
ConBT.setText("Confirm");
ConBT.setMargin(new Insets(2, 2, 2, 2));
ConBT.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ConBT_ActionPerformed(evt);
}
});
ConBT.setBackground(Color.WHITE);
cp.add(ConBT);
trenJL.setBounds(200, 0, 1, 350);
trenJL.setText("");
trenJL.setBackground(Color.BLACK);
trenJL.setOpaque(true);
cp.add(trenJL);
keyJL.setBounds(25, 130, 150, 30);
keyJL.setText("Key:");
keyJL.setBackground(Color.WHITE);
keyJL.setOpaque(true);
cp.add(keyJL);
errJL.setBounds(25, 220, 150, 30);
errJL.setText("");
errJL.setBackground(Color.WHITE);
errJL.setForeground(Color.RED);
errJL.setOpaque(true);
cp.add(errJL);
InoutTAScrollPane.setBounds(210, 10, 280, 310);
InoutTA.setText("");
InoutTA.setLineWrap(true);
InoutTA.setWrapStyleWord(true);
cp.add(InoutTAScrollPane);
setVisible(true);
}
public static void main(String[] args) {
new Vigenere("Vigenere").requestFocus();
}
public void ConBT_ActionPerformed(ActionEvent evt) {
requestFocus();
errJL.setText("");
error = false;
output = "";
check();
if(error == false){
allinfo();
if (casvigCB.getSelectedItem().equals("Caesar")){
if (Decrypt == false){
cryptcas();
}else{
decryptcas();
}
InoutTA.setText(output);
}else if(casvigCB.getSelectedItem().equals("Vigenére")){
if (Decrypt == false){
encode();
}else{
decode();
}
InoutTA.setText(output);
}
}
}
public void cryptcas(){
for(i=0; i< inarray.length; i++){
numchar = (int) inarray[i];
numtest();
if (testnum == true){
numchar = ((numchar-32) + numcae)%95 +32;
output += (char) numchar;
}else{
output += "¶";
}
}
}
public void decryptcas(){
for(i=0; i< inarray.length; i++){
numchar = (int) inarray[i];
numtest();
if (testnum == true){
numchar = ((numchar-32) - numcae)%95;
if(numchar < 0){
numchar += 95;
}
numchar += 32;
output += (char) numchar;
}else{
output += "¶";
}
}
}
// encode viginere
public void encode(char chr) {
int c = chr;
if (c> 64 && c < 91) {
int code = this.code.charAt(count) - 65;
c += code;
if (c> 90) {
c-= 26;
} // end of if
count = (count < this.code.length() -1) ? ++count : 0;
} // end of if
return (char) c;
}
// decode vigenere
public void decode(char chr) {
int c = chr;
if (c > 64 && c < 91) {
int code = this.code.charAt(count) - 65;
c += code;
if (c< 65) {
c += 26;
} // end of if
count = (count < this.code.length() - 1) ? ++count : 0;
} // end of if
return (char) c;
}
public void numtest(){
testnum = true;
if(numchar<32){
testnum = false;
errJL.setText("Unusual chars = ¶");
}else if (numchar > 126){
testnum = false;
errJL.setText("Unusual chars = ¶");
}
}
public void allinfo(){
TFkey = versTF.getText();
input = InoutTA.getText();
inarray = input.toCharArray();
if(verentCB.getSelectedItem().equals("Decode text")){
Decrypt = true;
}else if(verentCB.getSelectedItem().equals("Encode text")){
Decrypt = false;
}
if(casvigCB.getSelectedItem().equals("Caesar")){
numcae = Integer.parseInt(TFkey);
}else if(casvigCB.getSelectedItem().equals("Vigenére")){
keyarray = TFkey.toCharArray();
}
}
public void check(){
if(versTF.getText().equals("")){
errJL.setText("Missing key");
error = true;
}else if(InoutTA.getText().equals("")){
errJL.setText("Missing text");
error = true;
}else if(casvigCB.getSelectedItem().equals("Vigenére")){
keyarray = versTF.getText().toCharArray();
for(i=0; i<keyarray.length; i++){
if((keyarray[i] <32) || (keyarray[i]> 126)){
error = true;
errJL.setText("Choose other key");
}
}
}else if(casvigCB.getSelectedItem().equals("Caesar")){
try{
numcae = Integer.parseInt(versTF.getText());
}catch (Exception e){
errJL.setText("Caesar needs a number");
error = true;
}
if (numcae > 95){
errJL.setText("Number for Caesar < 96");
error = true;
}else if(numcae <= 0){
errJL.setText("Number for Caesar > 0");
error = true;
}
}
}
}
[/java]
Vielen Dank!