Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using getText to get an Integer

I am working on a program with in interface that uses textAreas in order to recieve user input and at one point when I use the getText() function it tells me that the variable that I am trying to declare with it, an integer, must be a string. Would there be any other way to change this or would there be another function that I would use to call in integer from a textArea?

Edit:

Sorry, I did leave out some code for an example.

public void SetStock(javax.swing.JTextArea textStock)
{
    Integer _textStock = textStock.getText();
}
public  Integer getTextStock()
{
    return _textStock;
}

This is the setter/getter that I am trying to use in order to use it within my main class.

like image 802
user2128212 Avatar asked Dec 15 '22 13:12

user2128212


1 Answers

You can use the text, and then parse it as an integer using

int i = Integer.parseInt(myText.getText());

also, Integer.parseInt can throw an exception if the text is not a valid integer, so you might want to surround that in a try/catch block and catch the exception NumberFormatException

like image 89
Ahmed Aeon Axan Avatar answered Dec 28 '22 07:12

Ahmed Aeon Axan