Working on a homework assignment and I’m absolutely horrible with programming. I need to run a program that asks a riddle and lists some words. the Answer to the riddle is the first letter of each word. Once the user inputs the correct answer into JOptionPane the screen is supposed to turn black. The part that I’m hung up on is getting the user input to turn the screen black and I can’t seem to find any tutorial videos that will talk about this using strings. I found one that uses int’s but I’m at a loss as to how to do this with strings.
color boxFill =color(0);
color letterFill = color (0, 200, 0);
size (500, 500);
void draw ()
void opener ()
String opener = "i am: ";
rect (width/2-textWidth(opener)/2, height/24+textAscent(),
opener = opener.toUpperCase();
text (opener, width/2, height/10);
String clues = ("one, two, three, four ");
clues = clues.toUpperCase();
String  list = clues.split(" ");
for (int i = 0; i < list.length; i++)
rect (width/2-textWidth(list[i])/2, height/7+i*50+textAscent(),
text (list[i], width/2, height/5+i*50);
void finalQuestion ()
String question = "Who am I?";
rect (width/2-textWidth(question)/2, height-height/3-textAscent(),
text (question, width/2, height-height/3);
String correct = "Correct";
String answer = "Answer?";
fill (255, 0, 0);
rect (width/2-textWidth(answer)/2, height-height/5-textAscent(),
text (answer, width/2, height-height/5);
if (mousePressed && mouseX >= 200 && mouseX <= 300 && mouseY >= 300 && mouseY <= 400)
String solution = "";
String input = javax.swing.JOptionPane.showInputDialog(null, answer);
First, you never want to use a single = equals sign in an if statement. That’s assigning a new value to the variable, not comparing the variable to the value.
Secondly, you actually (almost) never want to use == with String values at all. Instead, you want to use the equals() function. Here’s a simple example:
String one = "test";
String two = "test";
If I were you, I would try to work in smaller steps and test out the syntax in small example programs like the one I just posted. Then if you get stuck on one of those steps, you can post a MCVE along with a more specific technical question.
PS- You can preserve your code’s formatting in the forum by highlighting your code and clicking the </> code button. You can edit your post to format it now if you want.
I made some slight changes and have been playing around with it. I think I may have worded my question wrong, what I’m trying to do is get the what the user inputted as a solution so that it triggers and event to happen, in this case make the screen go black if the its the write answer
I’d use the draw() function to your advantage. Remember that the draw() function runs 60 times per second. You can store your program’s state in a set of variables at the top of your sketch. Use those variables to know what to draw each frame, and then change those variables to change what’s being drawn.
The draw() method runs forever, it should include code that you want to run infinitely (e.g. drawing objects, images). Instead you’re using it like the main method. I suggest you call your methods in setup().