Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting Boolean Method to Work With Java Swing Applet

 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:



How do I make it so that the boolean method works under the "convertNameActionPerformed" method?
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hasComma() isn't part of convertNameActionPerformed(), is it?  Have you tried putting hasComma() outside of convertNameActionPerformed()?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:hasComma() isn't part of convertNameActionPerformed(), is it?  Have you tried putting hasComma() outside of convertNameActionPerformed()?


Netbeans does not let me. Any other ideas? This is the compiler error.

 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would appear to be trying to write a method inside another method. That is not permitted.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I get the "hasComma" and "convertName" to work under the "convertNameActionPreformed"?
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess my question is, why wouldn't they work outside of convertNameActionPreformed?  Back up a bit and tell us what you're trying to do.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I guess my question is, why wouldn't they work outside of convertNameActionPreformed?  Back up a bit and tell us what you're trying to do.


My apologies. So what I need to do in this program is take a name ex. "Joe Smith" and convert it to "Smith, Joe". I have all the code correct (as I had previously created a project and just have to put it into applet form). My question is, how do I make it so that my String strName = inputName.getText(); line can work under/with public static String convertName(String name) . The problem that NetBeans is telling me is that String strName = inputName.getText(); is a "non-static variable inputName cannot be referenced from a static context". How would I fix this?

 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some reason why hasComma() and convertName() are static?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Is there some reason why hasComma() and convertName() are static?


That is just how I had it formatted when I made it regularly. Now I have to make it so that it can be used in an applet.
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (name == "") ??? (line 4) As you can see here, that test is bound to fail if an empty String is passed.
name.indexOf(',') >= 0 ??? (line 11) That will work, but it isn't elegant.
Both those bits of code shou‍ld be replaced by calls to methods of the String object which tell you whether a String is empty or whether that String contains another String.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:if (name == "") ??? (line 4) As you can see here, that test is bound to fail if an empty String is passed.
name.indexOf(',') >= 0 ??? (line 11) That will work, but it isn't elegant.
Both those bits of code shou‍ld be replaced by calls to methods of the String object which tell you whether a String is empty or whether that String contains another String.




This is my code now. It is all correct and NetBeans says it is as well. But when I run the java file, the java screen pops up but the program does not work when I hit the convert button.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What's the value of the argument being sent to convertName(name)?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
What's the value of the argument being sent to convertName(name)?


The inputed name that was converted.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I'm missing something, name is null.  Where does name get set except to null?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Unless I'm missing something, name is null.  Where does name get set except to null?


Name should be set to the newName but I am not sure how to do that. newName comes from the convertName method. How would I get that to work?
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me clear some code out of the way, and see if this makes sense:

What are you sending into convertName()?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Let me clear some code out of the way, and see if this makes sense:

What are you sending into convertName()?


I want to set the converted name into it.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

With this part of the code, you are setting the text of outputName, which I assume is a label.  Whatever "..." evaluates to will be what is set.

Now, "..." is this:

You are calling the method convertName and passing the argument name.

But there's a problem: name is null, not the name to convert.  You need to pass convertName the correct argument.

Here you are setting the variable strName to the text of inputName, which I'm assuming is a textField.  So what variable name should you pass to convertName?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
With this part of the code, you are setting the text of outputName, which I assume is a label.  Whatever "..." evaluates to will be what is set.

Now, "..." is this:

You are calling the method convertName and passing the argument name.

But there's a problem: name is null, not the name to convert.  You need to pass convertName the correct argument.

Here you are setting the variable strName to the text of inputName, which I'm assuming is a textField.  So what variable name should you pass to convertName?


This solved my problem! Thank you so much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic