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.

All things are lawful, but not all things are profitable.

Lexi Turgeon

Ranch Hand

Posts: 139

posted 1 month ago

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 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

posted 1 month ago

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

posted 1 month ago

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.