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

2013/9/9

Changing a string into an integer?

a_quiet_person a_quiet_person

2013/9/9

#
I have two actors which when clicked create an input box, one allows the user to enter a height (actor name= height) and one allows the user to enter a width (actor name= reply). Once these have been entered I wanted to use the user's answers and multiply them, then display this answer on the screen. The issue I am having is that I am not sure how to create variables for these answers and how to then multiply them (as they are recognised as strings) finally displaying this answer on the screen. Please help; The code I have for the actor 'Reply': import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.JOptionPane; import java.awt. Color; /** * Displays a string for the answer and when clicked will display an input box. * * @author * @version 1.0 */ public class Reply extends Actor { private String inputStr; private int buttonNbr; private Label phoneMsg; /** * Creates the text. */ public Reply() { setImage(new GreenfootImage("*click here to enter the width*", 20, Color.red, Color.white)); } /** * Creates an input box, when the text is clicked */ public void act() { if (Greenfoot.mousePressed(this)) { makeDialog(); } } /** * The code for the input box */ public void makeDialog() { inputStr = JOptionPane.showInputDialog( "Please enter the width in cm"); buttonNbr = JOptionPane.showConfirmDialog( null, "Are you sure?"); String msg = "The width is: " + inputStr + "cm"; JOptionPane.showMessageDialog(null, msg); } }
Gevater_Tod4711 Gevater_Tod4711

2013/9/9

#
To convert a String to an int you can use the method Integer.parseInt(String) which returns the int value of the String. This method throws a NumberFormatException so you need to surround it with a try catch block.
1
2
3
4
5
6
try {
    int value = Integer.parseInt(yourString);
}
catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}
MatheMagician MatheMagician

2013/9/9

#
You can use the method Integer.parseInt(); to convert strings to integers.
1
2
3
int widthINT = Integer.parseInt(width);
int heightINT = Integer.parseInt(height);
int product = widthINT*heightINT;
a_quiet_person a_quiet_person

2013/9/9

#
Thanks, got it to work now.
mjrb4 mjrb4

2013/9/12

#
Gevater_Tod4711 wrote...
To convert a String to an int you can use the method Integer.parseInt(String) which returns the int value of the String. This method throws a NumberFormatException so you need to surround it with a try catch block.
1
2
3
4
5
6
try {
    int value = Integer.parseInt(yourString);
}
catch (NumberFormatException nfe) {
    nfe.printStackTrace();
}
NumberFormatException is an unchecked exception, so you don't *need* to surround it with the try catch block (though you may wish to if you're parsing something that may not be a valid integer, user input for example.)
You need to login to post a reply.