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

2014/2/24

Print out an Input

Hersov Hersov

2014/2/24

#
This is my code, I want it to take in a sequence of numbers and then when I input it then print it out. this is what ive done so far but can someone help me finish it off
String inputstring1 = JOptionPane.showInputDialog("Enter Your Code Here");
        int number1=Integer.parseInt(inputstring1);
        
        GreenfootImage textImage = new GreenfootImage(number1 ,25, Color.WHITE, Color.BLACK);
            setImage(textImage);
danpost danpost

2014/2/24

#
I think the only problem with the code is that the GreenfootImage constructor you are using on line 4 requires a String as the first argument (not an int). Just change ' number1 ' to ' ""+number1 '.
Hersov Hersov

2014/2/24

#
This works, thank you but when I type it in It keeps coming back again. Here is my code again.
 String inputstring1 = JOptionPane.showInputDialog("Enter Your Code Here");
        int number1=Integer.parseInt(inputstring1);
        
        GreenfootImage textImage = new GreenfootImage(""+number1 ,25, Color.WHITE, Color.BLACK);
            setImage(textImage);
danpost danpost

2014/2/24

#
I hope you did not place this code in your 'act' method of the class without any conditionals. Will need to see context of given code to assist further. (method it is in and/or class code)
Hersov Hersov

2014/2/24

#
This is It, Yes it is in Act. What should it be in?
public void act() 
    {
        String inputstring1 = JOptionPane.showInputDialog("Enter Your Code Here");
        int number1=Integer.parseInt(inputstring1);
        
        GreenfootImage textImage = new GreenfootImage(" Your Code is"+number1 ,25, Color.WHITE, Color.BLACK);
            setImage(textImage);
    }    
danpost danpost

2014/2/24

#
Not enough info to instruct. It depends on what the input is used for and when the input is to be given compared to the rest of the class/project.
Hersov Hersov

2014/2/24

#
This is all of it, my intention is that the user will input a "code" and then the code to be displaed at the top (this is done and works) but sadely the input box keeps coming, it changes the code at the top but It keeps coming back over and over again. Hopefully this will be enough Information
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;
import java.awt.Color;
/**
 * Write a description of class Code here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Code extends Actor
{
    /**
     * Act - do whatever the Code wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        String inputstring1 = JOptionPane.showInputDialog("Enter Your Code Here");
        int number1=Integer.parseInt(inputstring1);
        
        GreenfootImage textImage = new GreenfootImage(" Your Code is"+number1 ,25, Color.WHITE, Color.BLACK);
            setImage(textImage);
    }    
}
danpost danpost

2014/2/24

#
OK, seeing that it is in an Actor class called Code, I can better instruct. You need to move the code from the act method to a Code constructor method: Or, just change 'public void act()' to 'public Code()'.
Hersov Hersov

2014/2/25

#
Thank you very much
You need to login to post a reply.