We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am making an simple encryption machine. You type in a word and it gets encrypted into something unreadable. I have tested it with just an letter and i works, but now that i'm trying to do it with a word, nothing comes out, even do it's the same code, just modified slightly.
My code:
import controlP5.*;
ControlP5 controlP5;
String headline = "Cryptography machine";
String output = "";
String word = "";
String letter [];
char character;
int mode;
int crypt;
String tempTemp[];
void setup()
{
size(700,400);
background(0);
controlP5 = new ControlP5(this);
PFont font = createFont("arial",20);
controlP5.addTextfield("plain text")
.setPosition(20, 100)
.setSize(500,45)
.setFont(font)
.setFocus(true)
.setColor(color(255,0,0));
controlP5.addTextfield("crypt text")
.setPosition(550, 100)
.setSize(45,45)
.setFont(font)
.setFocus(true)
.setColorBackground(color(0,0,255));
controlP5.addButton("Encrypt")
.setValue(1)
.setPosition(20,170)
.setSize(100,19)
.setColorBackground(color(255, 0, 0));
controlP5.addButton("Decrypt")
.setValue(2)
.setPosition(140,170)
.setSize(100,19)
.setColorBackground(color(255, 0, 0));
Button reset = controlP5.addButton("Reset")
.setValue(3)
.setPosition(260,170)
.setSize(100,19)
.setColorBackground(color(255, 0, 0));
reset.addCallback(new CallbackListener()
{
public void controlEvent(CallbackEvent theEvent)
{
switch(theEvent.getAction())
{
case(ControlP5.ACTION_PRESSED): mode = 3; break;
case(ControlP5.ACTION_RELEASED): mode = 0; break;
}
}
}
);
mode = 0;
}
void draw()
{
background(0);
fill (255);
text(output, 20,300);
// Overskrift.
textSize(40);
text(headline, 130, 60);
println(output);
encryptFunction();
switch(mode)
{
case 1:
encryptFunction();
break;
case 2:
encryptFunction();
break;
case 3:
resetFunction();
break;
}
}
public void Encrypt(int theValue) {
mode = theValue;
}
public void Decrypt(int theValue) {
mode = theValue;
}
public void encryptFunction()
{
word = controlP5.get(Textfield.class,"plain text").getText();
letter = word.split("");
for(int i = 0; i < letter.length; i++)
{
String temp = letter[i];
try
{
character = temp.charAt(0);
}
catch (StringIndexOutOfBoundsException e)
{
}
try
{
crypt = Integer.parseInt(controlP5.get(Textfield.class,"crypt text").getText());
}
catch (NumberFormatException e)
{
}
int ascii = (int) character;
int xor = ascii ^ crypt;
char encryptedWordChar = (char) xor;
String encryptedWord = Character.toString(encryptedWordChar);
try
{
tempTemp[i] = encryptedWord;
}
catch (NullPointerException e)
{
}
}
try
{
output = tempTemp.toString();
}
catch (NullPointerException e)
{
}
}
public void resetFunction()
{
}
The encryption is done in the metode - encryption.
Hope some one will help me.
Answers
you don't need split
this is how to do it
(this is actually a full working sketch)
Best, Chrisir ;-)
I get an
word.length cannot be resolved or is not a field
:(String datatype doesn't have any field called length.
Rather a method called length(): https://Processing.org/reference/String_length_.html
I'm still not getting an output. I have made the above changes. Why is it I don't need the .split function?
Does my example work for you?
split()
is for separating words with a delimiter (which is != empty afaik)To go through chars of a string do as I've shown (no
split()
needed)use
charAt()
I have implementet your code. It works but i'm still not getting anything at my output tag.
My encryption code now:
public void encryptFunction() { String encryptedWord; String tempTemp[] = null; char character; word = controlP5.get(Textfield.class,"plain text").getText(); for(int i = 0; i < word.length(); i++) { character = word.charAt(i); int ascii = (int) character; int xor = ascii ^ crypt; char encryptedWordChar = (char) xor; encryptedWord = Character.toString(encryptedWordChar); try { tempTemp[i] = encryptedWord;} catch (NullPointerException e) { } } try { output = tempTemp.toString();} catch (NullPointerException e){ } }
you need to debug
use
println()
and check each stepprintln (ascii);
eg - are the values as expected?word
?character
?you don't init tempTemp ( you could just add the result up btw.)
to init it say
tempTemp=new String[word.length()];
before for-loopdon't use
try catch
--- or useprintln
incatch
My problem is in this code:
try { tempTemp[i] = encryptedWord;} catch (NullPointerException e) { }
just say
result+=encryptedWord;
don't use
try
during debugStill no output. If i don't use "try" and "catch" I get a NullPointerException.
that's good
it's a lead.
comment out that line with
//
now
println
onencryptedWord
works?
you didnt tell if you used
result +=
or/and if you init the array
I can println encryptedWord.
I tried += but it didn't change anything.
result+=
and result is a String, not an array??
try
result+=encryptedWordChar; // with Char
at which line is nullpointer excep?
Okay, now it works, I don't understand why? I encrypt each letter induvidually, so why shouldn't I use an array then?
as I said you didn't init the array
Okay. Thank you very much for your help.
no problem
;-)
I have found a new problem. My for loop continuous forever. which means that it continues to output the same letter :-S I'm using the code
word = controlP5.get(Textfield.class,"plain text").getText();
to write.I have found the reason. I forgot to remove += from output. I was from a previous test :)
I see.