• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Name Swing Applet

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


This is my code. I must create a program that will take the inputted name and put it into standard form. I have many errors with my variables and my "nameInputActionPerformed" method. Does anyone know where I went wrong?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:I have many errors with my variables and my "nameInputActionPerformed" method. Does anyone know where I went wrong?


Well, there are certainly lots of compiler errors there... but instead of just telling you, how about you tell us what the compiler error message are? And of course, this is so that we can help you interpret it, and understand how to fix it.

Henry

PS.... welcome to the ranch.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My errors from my compiler are:

 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:My errors from my compiler are:


This is a syntax error. In this case, the compiler believes that this section of code is no longer defined in a class, and is expecting you to start a new class or interface definition.

This is generally caused by mismatched parenthesis. You probably have one too many close parenthesis, that closed the class definition itself.

Additionally, the compiler is completely confused at this point. So, the rest of the errors may not even be valid. Just work on the first compiler error.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have edited a few things but come up with some compiler issues that I do not understand.

Here is my code:


Here is what my compiler says is wrong:


 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error: reached end of file while parsing

Check that the {}s are properly paired.
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:I have edited a few things but come up with some compiler issues that I do not understand.

Here is what my compiler says is wrong:



Agree with Norm. This is also a syntax error. The compiler still thinks that the class isn't complete. This is also formed by mismatch parenthesis, but in this case, you don't have enough close parenthesis.

Henry
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:. . . Check that the {}s are properly paired.
The eaiest way to do that is to indent the code correctly, which you can probably do with a ctrl combination in NetBeans, and hover your mouse over a { and see which } changes colour. Your indentation in the posted code is not quite consistent and that makes it easy to mismatch {} without noticing.
Hint: class interface or enum exected at the end of the code: too many } or too few {
reached end of file…: too few } or too many {
class interface or enum expected at the start of the code: some sort of spelling error.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So after those second corrections I only have one problem left from my compiler.



And this is my updated code:



The line " nameOutput.setText(newName); " has the "newName" underlined in saying it is wrong. I am trying to call that from my "convertNameActionPerformed". Why is what I put wrong and how would I fix it?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
The line " nameOutput.setText(newName); " has the "newName" underlined in saying it is wrong. I am trying to call that from my "convertNameActionPerformed". Why is what I put wrong and how would I fix it?


The compiler is complaining that it can't find the declaration for "newName" anywhere. And looking at your code, the compiler is correct. You don't have any "newName" variable in scope for that line.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I declare it so that I can get it from the method above? Would I just call that method?
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is newName supposed to be?  Did you mean yourname?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, you were right. Now that my code is all working, the program does not run. Great.
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Does not run", does it get an error?  What is the error?  If not, what exactly happens?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try and run the program with no errors in the code this is what the compiler says is wrong:

 
Cooper Gates
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lexi,

Where is nameInputActionPerformed called? Judging by the
parameter you passed into it, you have the following import:

import java.awt.event.ActionEvent;

I bet the function line doesn't need all the . operators, it could be

private void nameInputActionPerformed(ActionEvent evt)

Not to mention, that function has no use of the variable evt, so it could
operate with no parameters.

I assume you have “nameInput” and “nameOutput” declared somewhere.
You also should check for a null before reaching into a member.
For now, I would write the function like this:

private void nameInputActionPerformed()
{

if (nameInput != null && nameOutput != null)
nameOutput.setText(nameInput.getText());

} // That's it. Check both variables for null, and you don't need
// to declare a string to hold getText(), it can go in immediately.
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cooper Gates wrote:. . . I bet the function line doesn't need all the . operators, it could be

private void nameInputActionPerformed(ActionEvent evt)
I think that is code written automatically by NetBeans and it defaults to using the fully‑qualified name of the class. It may be impossible to change that code without moving it out of NetBeans.
. . . You also should check for a null before reaching into a member. . . .
Why? The fields shou‍ld have been initialised to “real” values before that code it executed. If there is a null anywhere, it will be necessary to alter the source code to correct the error. I would think it is correct to wait for an exception and assume all is well if no exception is thrown.
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cooper Gates wrote:. . . you don't need to declare a string to hold getText(), it can go in immediately.
That is a style matter where there are differing opinions.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the code is generated when I create the design of the project I want. I thought maybe it was something that I did not initialize in the design. I am not sure what else it could be.
 
Campbell Ritchie
Sheriff
Posts: 51468
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:All the code is generated when I create the design of the project I want. . . .
Yes, all that code is generated automatically by the GUI designer when you give it the placement of the Components. When you click a component and tell it to add a Listener, it writes the appropriate method automatically. I think the GUI designer will cause most of that code to revert to its previous state if you try to change anything.
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to see the code for NewJApplet.init, especially line 47.
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cooper Gates wrote:
private void nameInputActionPerformed()
{

if (nameInput != null && nameOutput != null)
nameOutput.setText(nameInput.getText());

}

Could you please UseCodeTags (that's a link) when you post code?
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea how I would solve my errors?
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I'd like to see the code for NewJApplet.init, especially line 47.

Could you post the above code and we'll see what we can do.
 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I'd like to see the code for NewJApplet.init, especially line 47.


 
Lexi Turgeon
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help. I had selected the wrong item when formatting the GUI.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic