11 Replies - 18546 Views - Last Post: 10 December 2008 - 07:09 PM Rate Topic: -----

#1 Coder93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-December 08

How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 08:30 PM

I am trying to make a program similar to an ATM. I am trying to make a menu that allows you to enter account information and then go back and withdraw or deposit into an account. My problem is that i can't start the program because the info that would be entered into the "sub menu" could not be accessed by the other "sub menus."

Here is what I have so far....

 

import cs1.Keyboard;

public class JavaBank
{
	public static void main (String[] args)
   {
		  int input = 5, index, limit;
		  
			//Initialize arrays
			String[] firstName = new String[index];
			String[] lastName = new String[index];
			int[] accountNum = new int[index];
			double[] balance = new double[index];

		  
		 if (input == 5)
		 {		 
		 System.out.println ("~~~~Welcome to the Bank of Java~~~~");
	   System.out.println ();
		 System.out.println (" 1) Input Account Information");
	   System.out.println (" 2) Withdraw");
	   System.out.println (" 3) Deposit");
	   System.out.println (" 4) Check Balance");
	   System.out.println ();
		   
	   System.out.println ("What would you like to do?");
		 System.out.print ("Choose a number from the list: ");
	   input = Keyboard.readInt();
	   System.out.println ();
		 }
		 
		 if (input == 1)
		 	{
				System.out.println ("How many accounts would you like to input?");
				index =	Keyboard.readInt();
				limit = index;
						
				for (index = 0; index < limit; index++)
				{
					System.out.println ("Input account holder's first name");
					firstName[index] = Keyboard.readString();
		
					System.out.println ("Input account holder's last name");
					lastName[index] = Keyboard.readString();
		
					System.out.println ("Input account holder's account number");
					accountNum[index] = Keyboard.readInt();
					
					System.out.println ();
				}
			
				input = 5;
			}
			
		if (input == 2)
			{
				System.out.println ("Input the account number you wish to edit");
				int editAccount = Keyboard.readInt();
				
				for (index = 0; index < limit; index++)
				{	
					if (editAccount == accountNum[index])
						System.out.println (index);
						
				}
			}
	}
}





I need to be able to use the information entered into the arrays throughout the program.

I would greatly appreciate any help.

Is This A Good Question/Topic? 0
  • +

Replies To: How to Make a Variable Usable in Entire Program

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 08:52 PM

Declare the data items that need global access as class variables:
public class JavaBank
{
	//
	//declare your variables here
	//

	public static void main (String[] args)
   {


Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 08:53 PM

That's kind of contradictory. It would be class variables, rather then global variables. Globals have scope everywhere in every class, etc...
Was This Post Helpful? 0
  • +
  • -

#4 Coder93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-December 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 08:59 PM

I put my variables in the place that you suggested, but i got this error a lot: "non-static variable input cannot be referenced from a static context."
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 09:17 PM

View Postn8wxs, on 9 Dec, 2008 - 07:52 PM, said:

Declare the data items that need global access as class variables:
public class JavaBank
{
	//
	//declare your variables here
	//

	public static void main (String[] args)
   {



KYA, in the following post is right

but n8wxs gave you a good clue

so

public class JavaBank
{
	//
	//declare your variables here
	//

			   JavaBank() {	  // constructor here
				 ....
				 put the code you used to have in main
			   }

	public static void main (String[] args) {
				   new JavaBank();
				 }   // that's it main is done... its job just instantiate a new JavaBank object 
					  // that can see all JavaBank variables
	   
   {


Was This Post Helpful? 0
  • +
  • -

#6 Coder93  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-December 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 09:39 PM

I am very new to Java and my knowledge is very basic. Therefore, I have no idea what you did with that sample code. I don't know what a constructor is and I get confused with all the classes and objects. Please help me to understand what you are talking about...
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 09:49 PM

View PostKYA, on 9 Dec, 2008 - 07:53 PM, said:

That's kind of contradictory. It would be class variables, rather then global variables. Globals have scope everywhere in every class, etc...

You're right of course. Global is my COBOL background showing. :)

The point was that making the items class variables lets all the class methods use them, which would allow the OP to get on with the project. :)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to Make a Variable Usable in Entire Program

Posted 09 December 2008 - 09:53 PM

View Postn8wxs, on 9 Dec, 2008 - 08:49 PM, said:

View PostKYA, on 9 Dec, 2008 - 07:53 PM, said:

That's kind of contradictory. It would be class variables, rather then global variables. Globals have scope everywhere in every class, etc...

You're right of course. Global is my COBOL background showing. :)

The point was that making the items class variables lets all the class methods use them, which would allow the OP to get on with the project. :)

Today at my job, slot machines programming, I had to animate a bonus and wrote a method PerformAnim()
My boss came back to me "You're f**** Cobol programmer naming a method PerformXXXX" :D
Was This Post Helpful? 0
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to Make a Variable Usable in Entire Program

Posted 10 December 2008 - 12:48 AM

That should be 1000PerformAnim()

You never know when someone might drop the card deck!

:)
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: How to Make a Variable Usable in Entire Program

Posted 10 December 2008 - 01:27 AM

i think you should declare your variables static, then the whole class will have a copy of it
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: How to Make a Variable Usable in Entire Program

Posted 10 December 2008 - 08:33 AM

View Postn8wxs, on 9 Dec, 2008 - 09:49 PM, said:

View PostKYA, on 9 Dec, 2008 - 07:53 PM, said:

That's kind of contradictory. It would be class variables, rather then global variables. Globals have scope everywhere in every class, etc...

You're right of course. Global is my COBOL background showing. :)

The point was that making the items class variables lets all the class methods use them, which would allow the OP to get on with the project. :)


Yeah sorry, my C++ tendencies were kicking in. You can have "real" globals there.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to Make a Variable Usable in Entire Program

Posted 10 December 2008 - 07:09 PM

View Postmostyfriedman, on 10 Dec, 2008 - 12:27 AM, said:

i think you should declare your variables static, then the whole class will have a copy of it

:rolleyes: no need at all to declare them static this completly another issue
and your other statement is really misleading
The whole class won't have a copy of it.. the whole class will access the same instance of it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1