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

2014/2/8

Read User Input

basil60 basil60

2014/2/8

#
I want to capture user input ( for teaching purposes - not an actual application). I don't want to use the dialgoue scenario - I just want to KIS and use Java principles. I have:
import java.util.Scanner;
public class Input extends Actor
{
       private static Scanner scan; 
    public void act() 
    {
        // Add your action code here.
        input();
    } 
    public void input(){
      int a;
      float b;
      String s;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a string");
      s = in.nextLine();
      System.out.println("You entered string "+s);       
    }
}
The Greenfoot terminal opens, prompting for input, but that's all I can get it to do. Your help would be appreciated.
danpost danpost

2014/2/8

#
Would something like this be sufficient?
import greenfoot.*;
import java.awt.Color;

public class StringInputBox extends Actor
{
    static final int MAX_INPUT_LENGTH = 20;
    String text = "Enter a String";
    
    public StringInputBox()
    {
        updateImage();
        text = "";
    }
    
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(15*MAX_INPUT_LENGTH, 30);
        image.setColor(new Color(128, 0, 0));
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(3, 3, image.getWidth()-6, 24);
        GreenfootImage textImage = new GreenfootImage(text, 24, Color.black, null);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2);
        setImage(image);
    }

    public void act()
    {
        String key = Greenfoot.getKey();
        if (key == null) return;
        if ("enter".equals(key) && text.length() > 0)
        {
            System.out.println("You entered String "+text);
            text = "";
            updateImage();
            return;
        }
        if ("backspace".equals(key) && text.length() > 0) text = text.substring(0, text.length() - 1);
        if ("escape".equals(key)) text = "";
        if ("space".equals(key)) key = " ";
        if (key.length() == 1 && text.length() < MAX_INPUT_LENGTH) text += key;
        updateImage();
    }
}
basil60 basil60

2014/2/8

#
Thanks Danpost. More than sufficient!! I really appreciate the effort you've taken. But, is there any reason mine is not working? I understand it's largely not Greenfoot ( at this stage, for me that's ok)...but I would like the simplicity of just working with input, and not have to worry about validation etc. I'd like to demonstrate Java inputs and working with strings, without it looking overly complex for beginner programmers. I want to work (teach) with Greenfoot, but be able to explore some of the more standard Java code and principles..with beginners.
danpost danpost

2014/2/8

#
I adjusted the 'updateImage' method to the following:
private void updateImage()
{
    GreenfootImage image = new GreenfootImage(16*MAX_INPUT_LENGTH+32, 30);
    image.setColor(new Color(128, 0, 0));
    image.fill();
    image.setColor(Color.lightGray);
    image.fillRect(3, 3, image.getWidth()-6, 24);
    for (int i=0; i<text.length(); i++)
    {
        GreenfootImage chrImage = new GreenfootImage(""+text.charAt(i), 24, Color.black, null);
        image.drawImage(chrImage, 24+16*i-chrImage.getWidth()/2, 15-chrImage.getHeight()/2);
    }
    setImage(image);
}
I have not tried working with the Scanner class, so I cannot help you on that.
basil60 basil60

2014/2/8

#
Again, an awesome response. Thanks!!! Where can I +1 you?
danpost danpost

2014/2/8

#
I think that Greenfoot 'steals' the keystrokes for the 'getKey' and 'isKeyDown' functions and the Scanner class loses out on receiving them (I could be wrong). The way you can '+1' me is by looking at some of my scenarios. Click on my icon (the fractal design image) and then on 'show all' for the scenarios.
You need to login to post a reply.