Java Using GUIs: Setting String Variables from Inputs

  • Thread starter Thread starter hotcommodity
  • Start date Start date
  • Tags Tags
    String Variables
AI Thread Summary
When working with GUIs in Java, input from dialog boxes can be captured and assigned to variables. For integers and doubles, methods like Integer.parseInt() and Double.parseDouble() are used to convert string inputs into their respective numeric types. However, when dealing with string inputs, no conversion is necessary since a string can be directly assigned to a string variable. The correct approach is simply to use the assignment statement: String variable = name; instead of looking for a parsing method. Additionally, using the toString() method is relevant for converting objects to strings, but it is not needed for direct string assignments. The discussion highlights the importance of understanding the differences between data types and the appropriate methods for handling them in Java.
hotcommodity
Messages
434
Reaction score
0
When using GUIs, you can enter an integer or a double into an input dialog box, then you can set a variable equal to the input such as below:

int variable = Integer.parseInt(String name);

or

double variable = Double.parseDouble(String name);

If I enter a string into the dialog box, and I want to set a variable of the string type equal to that input, what code can I use that's similar to the code above? I can't seem to find anything in my textbook.

I'm looking for something like: String variable = String.parseString(String name);

Any help is appreciated.
 
Technology news on Phys.org
I'm not totally sure I understand the question, but I'm pretty sure that what you want is the

object.toString()

method.

ANY object in Java should have a toString() method (although sometimes it will not return what you expect) and so it is analogous to the Type.parse(object) methods..
 
I'm no expert on Java but I would think that the reason you NEED "Integer.parseInt(String name)" and "Double.parseDouble(String name)" is to change a string to an integer or double. You don't NEED a function to change a string to a string!

Just "String variable= name;" should work.
 
Thank you for the replies.

I think HallsofIvy hit the nail on the head. I was doing it assbackwards. I should have imported a GUI package, and used the methods within that package to display a string.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top