473,421 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

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.


Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2. NewAttempt.java
  3. ***********************************************************
  4. public class NewAttempt
  5.  {
  6.     public static menu chooseMenu = new menu();
  7.     public static worker doWork = new worker();
  8.     public static char menuChoice;
  9.  
  10.     public static void main(String[] args)
  11.     {
  12.         menu.menu(menuChoice);
  13.         System.out.println ("main  " + menuChoice);
  14.         worker.worker(menuChoice);
  15.     }
  16. }
Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2. menu.java
  3. ***********************************************************
  4. import javax.swing.JOptionPane;
  5.  
  6.  
  7.  
  8. public class menu
  9. {
  10.  
  11.     public static char menu(char menuChoice)
  12.     {
  13.         String menuChoiceString;
  14.         menuChoiceString = JOptionPane.showInputDialog
  15.         ("MENU\n"+
  16.          "Choose the letter corresponding to the type of file needed:\n"+
  17.             "A  - \"A\" students\n" +
  18.             "B  - \"B\" students\n" +
  19.             "C  - \"C\" students\n" +
  20.             "D  - \"D\" students\n" +
  21.             "F  - \"F\" students\n" +
  22.             "G  - \"Female\" students\n" +
  23.             "M  - \"Male\" students\n" +
  24.             "I  - \"Improving\" students\n" +
  25.             "L  - a list of \"All\" the students\n" +
  26.             "T  - students from a particular town\n" +
  27.             "X  - Exit the menu.\n");
  28.         menuChoice = menuChoiceString.charAt(0);
  29.         System.out.println ("menu  " + menuChoice);
  30.         return menuChoice;
  31.     }
  32.  
  33. }
Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2. worker.java
  3. ***********************************************************
  4. public class worker
  5. {
  6.  
  7.  
  8.     public static void worker(char menuChoice)
  9.     {
  10.     System.out.println ("worker  " + menuChoice);
  11.     }
  12. }
  13. ***********************************************************
  14.  
RESULTS

menu A
main
worker
Oct 19 '10 #1
1 1611
Nepomuk
3,112 Expert 2GB
In Java, you pass variables by value, not by reference. So, change your code to something like this:
Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2.    NewAttempt.java
  3.  
  4. ***********************************************************
  5.    public class NewAttempt
  6.    {
  7.    public static menu chooseMenu = new menu(); // as you're using a static class menu(), you don't need an object here.
  8.    public static worker doWork = new worker(); // same here
  9.    public static char menuChoice;
  10.  
  11.    public static void main(String[] args)
  12.    {
  13.       menuChoice = menu.menu();
  14.       System.out.println ("main  " + menuChoice);
  15.       worker.worker(menuChoice);
  16.    }
  17. }
Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2. menu.java
  3. ***********************************************************
  4. import javax.swing.JOptionPane;
  5.  
  6.  
  7.  
  8. public class menu
  9. {
  10.  
  11.     public static char menu()
  12.     {
  13.         String menuChoiceString;
  14.         menuChoiceString = JOptionPane.showInputDialog
  15.         ("MENU\n"+
  16.          "Choose the letter corresponding to the type of file needed:\n"+
  17.             "A  - \"A\" students\n" +
  18.             "B  - \"B\" students\n" +
  19.             "C  - \"C\" students\n" +
  20.             "D  - \"D\" students\n" +
  21.             "F  - \"F\" students\n" +
  22.             "G  - \"Female\" students\n" +
  23.             "M  - \"Male\" students\n" +
  24.             "I  - \"Improving\" students\n" +
  25.             "L  - a list of \"All\" the students\n" +
  26.             "T  - students from a particular town\n" +
  27.             "X  - Exit the menu.\n");
  28.         char menuChoice = menuChoiceString.charAt(0);
  29.         System.out.println ("menu  " + menuChoice);
  30.         return menuChoice;
  31.     }
  32. }
Also, it is rather unusual to use so many static objects. You may want to consider something like this:
Expand|Select|Wrap|Line Numbers
  1. ***********************************************************
  2. worker.java
  3. ***********************************************************
  4. public class worker
  5. {
  6.     public worker() // this is the constructor
  7.     { }
  8.  
  9.     public void printWorker(char menuChoice)
  10.     {
  11.     System.out.println ("worker  " + menuChoice);
  12.     }
  13. }
  14. ***********************************************************
Greetings,
Nepomuk
Oct 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
1
by: Mauro | last post by:
I want to send a value called country to a page page2.asp from page1.asp. I have a field to entry this text value (variable fname) like this: <td class="titulos" width="14%">Station Code: <input...
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
6
by: Leszek | last post by:
Hi. I wrote a script: function zmiana(ile){ while(document.getElementById('accomp').childNodes.length>1){ ostatni=document.getElementById('document.dane.accomp').lastChild;...
3
by: squash | last post by:
I have spent two hours trying to make sense of this script, called crazy.php. The output should be nothing because $cookie_password is nowhere defined in this script, correct? But it actually...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
5
by: Jeff | last post by:
ASP.NET 2.0 This code crashes. It generate this error: Value cannot be null. Parameter name: type I've created some custom web.config settings and this crash is related to accessing theme...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
3
by: | last post by:
I am enjoying making generalized methods to serve common needs, such as: ImageProcessing.MakeQuickResize(); ImageProcessing.Sharpen(); FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate()...
1
by: shapper | last post by:
Hello, I have the following: SelectList targets; UpdateModel(targets, new { "Targets" }); I get an error on second line over targets saying: Use of unassigned local variable 'targets'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.