• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a variable from the main method?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am sorry that I am asking so many questions but I need help with something. I need to know how to use a variable inside the main method inside another method. Here is my code



If I declare the variable outside the main method it will compile but it won't do anything when I run the code. Here are the errors I am getting in this version

" [line: 14]
Error: cannot find symbol
symbol: variable password
location: class passwordCodeDriver1

[line: 19]
Error: cannot find symbol
symbol: variable callMethods
location: class passwordCodeDriver1"

also I am not sure if this is required to fix this error but here is the other code involved with this project



So yeah if someone can show me how to use a method in the main method I would be very grateful. Thanks in advanced.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:
If I declare the variable outside the main method it will compile but it won't do anything when I run the code. Here are the errors I am getting in this version

" [line: 14]
Error: cannot find symbol
symbol: variable password
location: class passwordCodeDriver1

[line: 19]
Error: cannot find symbol
symbol: variable callMethods
location: class passwordCodeDriver1"


Local variables are only in scope for the block that they are declared in. So, line 14 and line 19, can't access local variables declared in another method.

The fix is simple. If you need variables declared in a scope greater than a single method, then do so. Some options are using instance or static variables.

Henry

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Sing wrote:
If I declare the variable outside the main method it will compile but it won't do anything when I run the code. Here are the errors I am getting in this version

" [line: 14]
Error: cannot find symbol
symbol: variable password
location: class passwordCodeDriver1

[line: 19]
Error: cannot find symbol
symbol: variable callMethods
location: class passwordCodeDriver1"


Local variables are only in scope for the block that they are declared in. So, line 14 and line 19, can't access local variables declared in another method.

The fix is simple. If you need variables declared in a scope greater than a single method, then do so. Some options are using instance or static variables.

Henry



Thanks for the help. That fixed my old error but when I try to add a JOptionPane to display the password I get two new errors. Here is my new code



and here are my new errors

" [line: 21]
Error: <identifier> expected

[line: 21]
Error: <identifier> expected"

I also tried putting the JOptionPane inside the encrypt method and when I did it compiled but no second message box ever came up. What should I do now?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:
" [line: 21]
Error: <identifier> expected

[line: 21]
Error: <identifier> expected"


Syntax error on line 21. Java statements, that are not declarations, must be in a method, constructor, or initializer. You just can't place Java statements anywhere.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!