This site requires JavaScript, please enable it in your browser!
Greenfoot back
Stillinski24
Stillinski24 wrote ...

2017/1/11

HELP! Confused on how to continue. if/then react to user input

Stillinski24 Stillinski24

2017/1/11

#
import greenfoot.*;
import javax.swing.JOptionPane;
/**
 * Ramya Raja
 */
public class SecondPageButton extends Actor
{
    public void act() 
    {
        Remove2ndButton();
        CreateUserInput();
    }
    public void Remove2ndButton()
    {
       if(Greenfoot.mouseClicked(this)){
            World Page2;
            Page2 = getWorld();
            Page2.removeObject(this);
            return;
       }
    }
    public void CreateUserInput()
    {
        if(Greenfoot.mouseClicked(this)){
            String inputValue = JOptionPane.showInputDialog("Type an episode number or title (Ex:Season 1 Episode 12 or Malec)");   
        }
    }
    public void Text()
    {
        if(inputValue = "Malec")
            /**
             * Code for Displaying an image
             */
danpost danpost

2017/1/11

#
One major problem is that in line 25, which gets the input and assigns it to a local String variable, the value of 'inputValue' is immediately lost when execution of the 'if' block has completed (at line 26). You either need to increase the scope of the variable by declaring it outside the method or pass the value to the 'Text' method:
// increasing the scope so Text has access to the value (Text would be called after calling CreateUserInput)
String inputValue;

public void CreateUserInput()
{
    if (Greenfoot.mouseClicked(this)){
        inputValue = JOptionPane.showInputDialog("Type an episode number or title (Ex:Season 1 Episode 12 or Malec)");
    }
}

// OR, passing the value (Text is called within CreateUserInput
public void CreateUserInput()
{
    if (Greenfoot.mouseClicked(this)){
        String inputValue = JOptionPane.showInputDialog("Type an episode number or title (Ex:Season 1 Episode 12 or Malec)");
        Text(inputValue);
    }
}

public void Text(String inputValue)
{
    if ("Malec".equals(inputValue))
The last line shows how to compare two different String objects. Only use '==' on objects when you want to know if both references refer to the same object.
Stillinski24 Stillinski24

2017/1/12

#
Thank you so much! Great Help!
Stillinski24 Stillinski24

2017/1/13

#
My code will still not compile. I have tested each part and come to realize the method "Text" is not correct. Thank you in advanced!
public void act() 
    {
        Remove2ndButton();
        CreateUserInput();
        Text();
    }
    public void Remove2ndButton()
    {
       if(Greenfoot.mouseClicked(this)){
            World Page2;
            Page2 = getWorld();
            Page2.removeObject(this);
            return;
        } 
    }
       public void CreateUserInput()
    {
        if(Greenfoot.mouseClicked(this)){
            String inputValue = JOptionPane.showInputDialog("Type an episode number or title (Ex:Season 1 Episode 12 or Malec)");   
            Text(inputValue);
           }
    }
    public void Text(String inputValue)
    {
        if("Malec".equals(inputValue)){
             getWorld().setBackground(new GreenfootImage("Image.png"));
             getWorld().addObject(new Malec(), 300, 200);
        }
     }
}
Super_Hippo Super_Hippo

2017/1/13

#
If the code doesn't compile, it should tell you the error. You don't have to test anything to find it. You have to remove line 5.
Stillinski24 Stillinski24

2017/1/13

#
But if I remove line five the method "Text " will not be called
Super_Hippo Super_Hippo

2017/1/13

#
It is called in line 20. In line 19, you create the 'inputValue' String which you pass to the 'Text' method in line 20.
Stillinski24 Stillinski24

2017/1/16

#
Thank you; however, even though the code compiles it acomplishes nothing and a terminal window appears
Super_Hippo Super_Hippo

2017/1/16

#
What is the error message?
danpost danpost

2017/1/16

#
Stillinski24 wrote...
Thank you; however, even though the code compiles it acomplishes nothing and a terminal window appears
You should probably call 'Remove2ndButtton' after calling 'CreateUserInput' in the act method.
Stillinski24 Stillinski24

2017/1/17

#
Thank you so much @danpost and @Super_Hippo my code worked!
You need to login to post a reply.