Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting JTextField input to integer type

Status
Not open for further replies.

rdg2cig

Programmer
Sep 16, 1999
22
US
I am creating a small application that utilizes the jdbc:eek:dbc bridge to retrieve information from a database. I am new to Java and have mostly used C++ for text-based programs. Therefore, using the GUI classes is new to me. I created a JFrame that includes several JTextFields to receive input from the user. The input to JTextField tf1 will be an integer number representing the primary key of a database record. My problem is that the database views this field as a number and requires it to be a number in the sql query but the JTextField provides a String. How can I convert the String from the JTextField to a number (integer) so the database will recognize it in the SQL select statement? A snippet of the code follows. As you can see I tried casting to an int and that did not work. SearchResults is another JFrame that is called to display the results of the query. The resourceIN is the name of the number field representing the primary key in the database. Somehow I need to get the return from the JTextField tf1 into number format. Thank you for your assistance.<br>
<br>
(CODE FOR CLASS UP HERE)<br>
<br>
public void actionPerformed(ActionEvent e) {<br>
String queryString;<br>
if (e.getActionCommand().equals(CANCEL)) {<br>
setVisible(false);<br>
}<br>
else{<br>
if(e.getSource().equals(tf1)){<br>
// if (e.getActionCommand().equals(tf1)) {<br>
// String RIN = (int)tf1.getText(); DID NOT WORK<br>
queryString = &quot;SELECT resources.resourceIN,&quot; +<br>
&quot;resources.title, resources.location, &quot; + <br>
&quot;resources.available &quot;<br>
+ &quot;FROM resources WHERE &quot;<br>
// + &quot;resources.resourceIN = '&quot; + RIN + &quot;'&quot;; (DIDN'T <br>
// WORK) <br>
+ &quot;resources.resourceIN = '&quot; + tf1.getText() + &quot;'&quot;; <br>
/*<br>
queryString = &quot;SELECT resources.resourceIN, resources.title,&quot; + &quot; resources.location, resources.available &quot;<br>
+ &quot;FROM resources&quot;;<br>
*/ <br>
searchResults = new SearchResults(&quot;Library System &quot;<br>
+ &quot;-- Search Results&quot;, queryString); <br>
searchResults.pack();<br>
searchResults.setVisible(true);<br>
setVisible(false); <br>
}<br>
else{<br>
(MORE CODE FROM HERE)<br>

 
Hi!<br>
<br>
In this case you do not need to convert tf1.getText() to int, because you make the string manually (no paramter passing). Your mistake is, that use ', but the field is integer (you said).<br>
Try this:<br>
queryString = &quot;SELECT resources.resourceIN,&quot; +<br>
&quot;resources.title, resources.location, &quot; + <br>
&quot;resources.available &quot;+<br>
&quot;FROM resources WHERE &quot;+<br>
&quot;resources.resourceIN = &quot;+tf1.getText(); <br>
<br>
Good luck. Bye, Otto.<br>

 
// assume that JTextField field<br><br>int value;<br>field.getText(String.valueOf(value));<br><br>you put String.valueOf(int) inside the getText() method.<br><br>that would convert an int to a String.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top