We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,809 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do I use a string input in multiple methods?

Hi guys

I'm trying to take a user input (name) and replicate the input in other methods. Can anybody help? Here is the code:

import javax.swing.JOptionPane;

public class example

{	
	public static void main (String[]args)
		{
			entername();
		}
		
		public static void entername()
		{
			String a="";
			a = JOptionPane.showInputDialog("Welcome. Enter name ");
		
			mainscreen();
		
		} 	
		
		public static void mainscreen()
		{
		 	
			JOptionPane.showMessageDialog(null, "Thankyou " + a); //I want the String a input from entername() to appear here + a
			intro();
		}

		public static void intro()
		{
	
			JOptionPane.showMessageDialog(null, "Welldone " + a); //I want the String a input from entername() to appear here + a  
		
		}
	
				
}

And here are the errors:

C:\Documents and Settings\Adam\My Documents\Uni 2010\ComPuTer SciEncE\Java\example.java:24: cannot find symbol
symbol : variable a
location: class example
JOptionPane.showMessageDialog(null, "Welldone " + a); //I want the String a input from entername() to appear here + a
^
C:\Documents and Settings\Adam\My Documents\Uni 2010\ComPuTer SciEncE\Java\example.java:31: cannot find symbol
symbol : variable a
location: class example
JOptionPane.showMessageDialog(null, "Thankyou " + a); //I want the String a input from entername() to appear here + a
^
2 errors

Process completed.


Thanks guys!

3
Contributors
4
Replies
3 Hours
Discussion Span
2 Years Ago
Last Updated
5
Views
Question
Answered
adanaz
Newbie Poster
10 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

As you have it now, a is a local variable of your entername() method. That means it only exists on the local stack of that method, and when that method exits, the variable no longer exists.

If you declare a as a class field, it'll be acessible to your other methods:

public class foo{
  public String a;

  public static void main(String args[])
  {
   // cannot refer to a, main is static
  }

  public void bar()
  {
   // can read and write to a
  }
  
  public void frotz(String a)
  {
   // if you refer to a here, it's the one declared in the method body
   // to use the class field, refer to this.a:
 
   this.a = a;   //sets the class's a to whatever the input parameter is
                 //(frotz would be better called "setA")
  }
  
}
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187
Skill Endorsements: 3

jon.kiparsky's approach of using a class member is the one that I would have used. There is another choice - in case you insist not to have class members, you can have your method return the String back to the main method, and from there you can send it to other methods.

public static String entername() //we have changed the return value of the method to String
{
   String a="";
   a = JOptionPane.showInputDialog("Welcome. Enter name ");
   return a; //return the value back to main
}

In the main method, you can use the String as a parameter to send to the other methods.

public static void main(String args[])
{
   String name = entername(); //name will be equal to the value returned from entername()
   mainscreen(name); //now the method mainscreen will have the String name and will be able to use it.
}

Of course, you will have to change the mainscreen method to accept a parameter:

public static void mainscreen(String name)
{
   // implement
}
apines
Practically a Master Poster
633 posts since Apr 2007
Reputation Points: 129
Solved Threads: 55
Skill Endorsements: 0

Thanks alot. You explained it using my actual code which made it so easy. Thanks again! Now I can move on.

adanaz
Newbie Poster
10 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Thanks for your help guys.

adanaz
Newbie Poster
10 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by apines and jon.kiparsky

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0734 seconds using 2.7MB