r/processing • • Nov 30 '21

Problems with Strings

Post image
0 Upvotes

8 comments sorted by

4

u/ParthTatsuki Nov 30 '21

It is unclear what you're trying to do? Are you trying to associate a single key press with the variable dog1? If so, that's not the way to go. The 'key' will give you the key that you pressed, like 'c', not an entire string

1

u/coded-123 Nov 30 '21

No what I’m trying to do is to ask the user a question . For example, I’m building a guessing game; I’ll ask the user what animal image I displayed on the screen and then I compare the user input to my stored variable; if it correct, I type in correct else I’ll end the program

7

u/ParthTatsuki Nov 30 '21

Then what I'd suggest is start by having a blank string, everytime the user presses a key, you append the key to the string. If the key pressed is the "enter" key, you compare the string to the variable and display correct or incorrect. That's the easy way to do it. There may be better ways to do it but it's one of the simplest to start with.

2

u/ChuckEye Nov 30 '21

key is only one key, not a string.

1

u/coded-123 Nov 30 '21

What do I need use instead of key, I’ve looked on processing website but I don’t see what to use on keyboard function

7

u/[deleted] Nov 30 '21

Oi! It's better to copy / paste your code into a "Code Block"

//string to be inputed
String dog1 = "dog";
...

And copy the error message too

6

u/emilfranord Nov 30 '21

The key variable has the type char, a primitive used to store a singe character symbol. Therfore you can't compare a String to a char, because the are fundamentaly diffrent. Besides, how would you determine if cat is equal to a single keystroke, like r, or c.

It would really bennefit your post, if you wrote out the relevant code using Reddits code block system, instead of a picture of a screen displaying text.