How to get the variable value to pass to other methods.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Susan Clark

    How to get the variable value to pass to other methods.

    I am new to java but not new to programming.
    I can get everything in my programs to work except for passing the variable. And unless I can resolve this I cannot get any of my programs to run.

    I cannot get the value of my variable menuChoice to pass to other methods.


    Code:
    ***********************************************************
    NewAttempt.java
    ***********************************************************
    public class NewAttempt
     {
        public static menu chooseMenu = new menu();
    	public static worker doWork = new worker();
    	public static char menuChoice;
    
        public static void main(String[] args)
        {
        	menu.menu(menuChoice);
        	System.out.println ("main  " + menuChoice);
        	worker.worker(menuChoice);
        }
    }
    Code:
    ***********************************************************
    menu.java
    ***********************************************************
    import javax.swing.JOptionPane;
    
    
    
    public class menu
    {
    
    	public static char menu(char menuChoice)
    	{
    		String menuChoiceString;
    		menuChoiceString = JOptionPane.showInputDialog
    		("MENU\n"+
    	 	"Choose the letter corresponding to the type of file needed:\n"+
    			"A  - \"A\" students\n" +
    			"B  - \"B\" students\n" +
    			"C  - \"C\" students\n" +
    			"D  - \"D\" students\n" +
    			"F  - \"F\" students\n" +
    			"G  - \"Female\" students\n" +
    			"M  - \"Male\" students\n" +
    			"I  - \"Improving\" students\n" +
    			"L  - a list of \"All\" the students\n" +
    			"T  - students from a particular town\n" +
    			"X  - Exit the menu.\n");
    		menuChoice = menuChoiceString.charAt(0);
    		System.out.println ("menu  " + menuChoice);
    		return menuChoice;
    	}
    
    }
    Code:
    ***********************************************************
    worker.java
    ***********************************************************
    public class worker
    {
    
    
    	public static void worker(char menuChoice)
    	{
    	System.out.println ("worker  " + menuChoice);
    	}
    }
    ***********************************************************
    RESULTS

    menu A
    main
    worker
    Last edited by MMcCarthy; Oct 19 '10, 02:14 AM. Reason: added code tags
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    In Java, you pass variables by value, not by reference. So, change your code to something like this:
    Code:
    ***********************************************************
       NewAttempt.java
    
    ***********************************************************
       public class NewAttempt
       {
       [b]public static menu chooseMenu = new menu(); // as you're using a static class menu(), you don't need an object here.
       public static worker doWork = new worker(); // same here[/b]
       public static char menuChoice;
    
       public static void main(String[] args)
       {
          [b]menuChoice = menu.menu();[/b]
          System.out.println ("main  " + menuChoice);
          worker.worker(menuChoice);
       }
    }
    Code:
    ***********************************************************
    menu.java
    ***********************************************************
    import javax.swing.JOptionPane;
     
     
     
    public class menu
    {
     
        [b]public static char menu()[/b]
        {
            String menuChoiceString;
            menuChoiceString = JOptionPane.showInputDialog
            ("MENU\n"+
             "Choose the letter corresponding to the type of file needed:\n"+
                "A  - \"A\" students\n" +
                "B  - \"B\" students\n" +
                "C  - \"C\" students\n" +
                "D  - \"D\" students\n" +
                "F  - \"F\" students\n" +
                "G  - \"Female\" students\n" +
                "M  - \"Male\" students\n" +
                "I  - \"Improving\" students\n" +
                "L  - a list of \"All\" the students\n" +
                "T  - students from a particular town\n" +
                "X  - Exit the menu.\n");
            [b]char menuChoice = menuChoiceString.charAt(0);[/b]
            System.out.println ("menu  " + menuChoice);
            return menuChoice;
        }
    }
    Also, it is rather unusual to use so many static objects. You may want to consider something like this:
    Code:
    ***********************************************************
    worker.java
    ***********************************************************
    public class worker
    {
        public worker() // this is the constructor
        { }
    
        public void printWorker(char menuChoice)
        {
        System.out.println ("worker  " + menuChoice);
        }
    }
    ***********************************************************
    Greetings,
    Nepomuk

    Comment

    Working...