# Help with loops

**URL:** <https://discourse.processing.org/t/help-with-loops/1190>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [June 23, 2018, 8:16pm UTC](https://discourse.processing.org/t/help-with-loops/1190 "2018-06-23T20:16:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NNY](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NNY](https://discourse.processing.org/u/NNY)\
**Post date:** [June 23, 2018, 8:16pm UTC](https://discourse.processing.org/t/help-with-loops/1190/1 "2018-06-23T20:16:09Z")

</div>

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.

```auto
color boxFill =color(0);
color letterFill = color (0, 200, 0);
String input;

void setup()
{
  size (500, 500);
  textSize (40);
  rectMode (CORNERS);
  textAlign (CENTER);
}

void draw ()
{  
  background (100);
  opener ();
  clues();
  finalQuestion();  
  answerButton();
}

void opener ()
{
  String opener = "i am: ";
  fill (boxFill);
  stroke (0);
  rect (width/2-textWidth(opener)/2, height/24+textAscent(), 
    width/2+textWidth(opener)/2, height/24-textDescent());
  fill (letterFill);

  opener = opener.toUpperCase();
  text (opener, width/2, height/10);
}

void clues()
{
  stroke (0);
  String clues = ("one, two, three, four ");
  clues = clues.toUpperCase();
  String [] list = clues.split(" ");
  for (int i = 0; i < list.length; i++)
  {
    fill (boxFill);
    rect (width/2-textWidth(list[i])/2, height/7+i*50+textAscent(), 
      width/2+textWidth(list[i])/2, height/7+i*50-textDescent());
    fill (letterFill);
    text (list[i], width/2, height/5+i*50);
  }
}

void finalQuestion ()
{
  String question = "Who am I?";
  stroke (0);
  fill (boxFill);
  rect (width/2-textWidth(question)/2, height-height/3-textAscent(), 
    width/2+textWidth(question)/2, height-height/3+textDescent());
  fill (letterFill);
  text (question, width/2, height-height/3);
}

void answerButton()
{
  String correct = "Correct";
  String answer = "Answer?";
  fill (255, 0, 0);
  stroke (255);
  rect (width/2-textWidth(answer)/2, height-height/5-textAscent(), 
    width/2+textWidth(answer)/2, height-height/5+textDescent());
  fill (255);
  text (answer, width/2, height-height/5);
  if (mousePressed && mouseX >= 200 && mouseX <= 300 && mouseY >= 300 && mouseY <= 400) 
  {    
    String solution = "";
    while (!solution.equals("OTTF"))
    {
      String input = javax.swing.JOptionPane.showInputDialog(null, answer);
      if (input.equals("OTTF"))
      {
        background (0);
        noLoop();
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [June 23, 2018, 9:23pm UTC](https://discourse.processing.org/t/help-with-loops/1190/2 "2018-06-23T21:23:24Z")

</div>

This line jumps out at me:

> [@NNY](#):
>
> if (input = solution)

A couple things here:

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:

```auto
String one = "test";
String two = "test";

if(one.equals(two)){
  println("equal");
}

```

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](https://stackoverflow.com/help/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.

Good luck!

---

<div class="post-metadata">

**Author:** ![NNY](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NNY](https://discourse.processing.org/u/NNY)\
**Post date:** [June 23, 2018, 9:36pm UTC](https://discourse.processing.org/t/help-with-loops/1190/3 "2018-06-23T21:36:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [June 23, 2018, 9:41pm UTC](https://discourse.processing.org/t/help-with-loops/1190/4 "2018-06-23T21:41:12Z")

</div>

Which part of that is giving you trouble?

If I were you I’d start with a more basic example: can you write a simple sketch that gets a value from a user and prints it out?

Then work your way forward from there. Can you make it so the screen shows black after the user enters a specific value?

---

<div class="post-metadata">

**Author:** ![NNY](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@NNY](https://discourse.processing.org/u/NNY)\
**Post date:** [June 23, 2018, 9:47pm UTC](https://discourse.processing.org/t/help-with-loops/1190/5 "2018-06-23T21:47:19Z")

</div>

Everything works, what I can’t figure out is how to take what the user inputs into the JOptionPane and then input it into a loop.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 23, 2018, 9:50pm UTC](https://discourse.processing.org/t/help-with-loops/1190/6 "2018-06-23T21:50:04Z")

</div>

> <https://gist.github.com/GoToLoop/bba0c288aaeeb5ef9bb1>

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [June 23, 2018, 9:51pm UTC](https://discourse.processing.org/t/help-with-loops/1190/7 "2018-06-23T21:51:06Z")

</div>

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.

Here’s a simple example:

```auto
boolean drawRed = false;

void draw(){
  if(drawRed){
    background(255, 0, 0);
  }
  else{
    background(0, 255, 0);
  }
}

void mousePressed(){
  drawRed = true;
}

```

---

<div class="post-metadata">

**Author:** ![VansiusProductions](https://avatars.discourse-cdn.com/v4/letter/v/4da419/32.png) [@VansiusProductions](https://discourse.processing.org/u/VansiusProductions)\
**Post date:** [June 23, 2018, 11:59pm UTC](https://discourse.processing.org/t/help-with-loops/1190/8 "2018-06-23T23:59:21Z")

</div>

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().
