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!

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

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

Thanks for your help guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.