Using GUIs: Setting String Variables from Inputs

  • Context: Java 
  • Thread starter Thread starter hotcommodity
  • Start date Start date
  • Tags Tags
    String Variables
Click For Summary

Discussion Overview

The discussion revolves around how to set string variables from user inputs in GUI applications using Java. Participants explore methods for handling string inputs and clarify the need for specific parsing functions for different data types.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant inquires about how to assign a string variable from a GUI input, suggesting a method similar to existing parsing methods for integers and doubles.
  • Another participant proposes using the object.toString() method as a potential solution, although they express some uncertainty about the question.
  • A different participant argues that converting a string to a string does not require a specific function, suggesting that a simple assignment like String variable = name; should suffice.
  • A later reply acknowledges the previous responses and indicates a realization that the approach taken was incorrect, suggesting the need to import a GUI package for proper string handling.

Areas of Agreement / Disagreement

Participants express differing views on whether a specific method is needed to assign a string variable from input, with some suggesting that a simple assignment is adequate while others propose using existing methods.

Contextual Notes

The discussion does not resolve the question of best practices for handling string inputs in GUIs, and assumptions about the context of the GUI implementation remain unclear.

Who May Find This Useful

Java developers, particularly those working with GUI applications, may find this discussion relevant for understanding string handling from user inputs.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
55
Views
7K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
8K
  • · Replies 47 ·
2
Replies
47
Views
5K
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K