1.11M Members

How do I use a string input in multiple methods?

 
0
 

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!

 
1
 

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")
  }
  
}
 
0
 

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

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

 
0
 

Thanks for your help guys.

Question Answered as of 3 Years Ago by apines and jon.kiparsky
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: