Using GUIs: Setting String Variables from Inputs

  • Context: Java 
  • Thread starter Thread starter hotcommodity
  • Start date Start date
  • Tags Tags
    String Variables
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
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.
 
Physics 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.