Can't get an output!

edited August 2015 in Library Questions

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

  • edited July 2015

    you don't need split

    this is how to do it

    // word = controlP5.get(Textfield.class,"plain text").getText();
    
    String word =  "Hello"; 
    
    
    for (int i = 0; i < word.length (); i++)
    {
      println (word.charAt(i));
    }
    

    (this is actually a full working sketch)

    Best, Chrisir ;-)

  • I get an word.length cannot be resolved or is not a field :(

  • edited August 2015 Answer ✓

    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?

  • edited August 2015

    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)

  • edited August 2015

    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){ } }

  • edited August 2015

    you need to debug

    use println() and check each step

    println (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-loop

    don't use try catch --- or use println in catch

  • My problem is in this code:

    try { tempTemp[i] = encryptedWord;} catch (NullPointerException e) { }

  • edited August 2015

    just say

    result+=encryptedWord;

  • edited August 2015

    don't use try during debug

  • Still no output. If i don't use "try" and "catch" I get a NullPointerException.

  • edited August 2015

    that's good

    it's a lead.

    comment out that line with //

    now println on encryptedWord

    works?

  • edited August 2015

    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.

  • edited August 2015 Answer ✓

    result+=

    and result is a String, not an array??

  • edited August 2015 Answer ✓

    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

    ;-)

  • edited August 2015

    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 :)

Sign In or Register to comment.