• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling a variable from the main method?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic