Results 1 to 8 of 8
  1. #1
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Calling Variables

    Hey guys this is my first post so please let me know if I make any mistakes or you need more information.

    I have a test class that is testing some new classes I created however when I create an instance of the object "SAccount" I cannot call the variable it is stored in later in the same test program.

    It kinda goes like this:

    while (true) {
    if (true) {
    SAccount myvar = new SAccount();
    }
    }
    while (true) {
    if (true){

    }
    else if (true) {
    Adjustment(myvar);
    }
    }

    When I try to compile this it gives me:
    Test.java:59: cannot find symbol

    Adjustment(myvar);
    ^

    Thanks in advance,
    Nate

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Calling Variables

    One problem is that myvar is declared inside of a block of code, an if block to be exact, and is only visible within that block. Outside of that block it doesn't exist. Also, what is "Adjustment"? Is it a method? If so, please rename it to "adjustment" as method and variable names should begin with a lower-case letter.

  3. #3
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Re: Calling Variables

    Adjustment another class file within the package.
    Basically I only need that variable if the user requests it. What would be the appropriate way to do this? I put it in the if block, which as noted does not work.

    Is their a way to create the variable and then maybe use an if statement to activate it? Or something similar?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Calling Variables

    Quote Originally Posted by Soulpole View Post
    Adjustment another class file within the package.
    If Adjustment is class, then Adjustment(...) has no meaning since you don't create objects this way. Do you mean to create an Adjustment variable and initialize it with a new Adjustment object? If so then you should do something like this. i.e.,

    Java Code:
    Adjustment adjustment = new Adjustment(fubar);
    Basically I only need that variable if the user requests it. What would be the appropriate way to do this? I put it in the if block, which as noted does not work.

    Is their a way to create the variable and then maybe use an if statement to activate it? Or something similar?
    I'm still not quite sure what your code does as it's all quite confusing, not the least of which those infinitely repeating while (true) loops. I think that you should clarify your code and your problem a bit more.

  5. #5
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Re: Calling Variables

    I'm sorry for my unclear questions. I think that the "Adjustments" class should be a method in the Test program. However I moved it out so I could try to narrow down my problems and get the first part working.

    Line 58 and 61 are trying to call the variables that don't exist...
    Line 43 and 47, I need those variables to be available to Lines 58 and 61
    I cannot think of any ways around making the if statements create the variables in Lines 43 and 47.
    Hope this clears up my question.

    Thanks again,
    Nate

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Test {
      public static void main(String[] args) {
        boolean savingsYes = false;
    	 boolean checkingYes = false;
    	 int option1 = JOptionPane.YES_OPTION;
    	 int option2 = JOptionPane.YES_OPTION;
    	 String savingsString = "";
    	 String checkingString = "";
    	 String q1 = "Would you like to open an account?";
    	 String q2 = "For withdraw press 1\nFor deposit press 2\nFor balance inquiries press 3";
    	 String q3 = "Would you like to make another transaction?";
    	 String q4 = "Checking press 1\nSavings press 2?";
    	 
    	 option1 = JOptionPane.showConfirmDialog(null, q1);
    
    	 while (option1 == JOptionPane.YES_OPTION) {
    		if (!savingsYes && !checkingYes) {
    		  savingsString = "For Savings press 1";
    		  checkingString = "For Checking press 2";
    		}
    		else if (!checkingYes && savingsYes) {
    		  checkingString = "For Checking press 2";
    		  savingsString = "";
    		}
    		else if (!savingsYes && checkingYes) {
    		  savingsString = "For Savings press 1";
    		  checkingString = "";
    		}
    		else if (checkingYes && savingsYes) {
    		  JOptionPane.showMessageDialog(null, "You already have a Checking and Savings account");
    		  option1 = JOptionPane.NO_OPTION;
    		  continue;
    		}
    		else {
    		  JOptionPane.showMessageDialog(null, "Invalid Option");
    		}
    		q1 = "Would you like to open another account?";
    		String chooseString = JOptionPane.showInputDialog(savingsString + "\n" + checkingString);
    		int choose = Integer.parseInt(chooseString);
    		if (choose == 1) {
    		  SavingsAccount savings = new SavingsAccount(01, 0);
    		  savingsYes = true;
    		}
    		else if (choose == 2) {
    		  CheckingAccount checking = new CheckingAccount(02, 0);
    		  checkingYes = true;
    		}
    		option1 = JOptionPane.showConfirmDialog(null, q1);
    	 }
    	 
    	 while (option2 == JOptionPane.YES_OPTION) {
    	   if (savingsYes && checkingYes) {
    		  System.out.println("decide");
    		}
    		else if (savingsYes && !checkingYes) {
    		  Adjustments(savings);
    		}
    		else if (checkingYes && !savingsYes) {
    		  Adjustments(checking);
    		}
    		else if (!savingsYes && !checkingYes) {
    		  JOptionPane.showMessageDialog(null, "You do not have an account");
    		  System.exit(0);
    		}
    		option2 = JOptionPane.showConfirmDialog(null, "ut ho");
        }
      }
    }
    
    
    import javax.swing.JOptionPane;
    
    public class Adjustments {
    
      public Adjustments(Account acc) {
        if (acc instanceof SavingsAccount) {
          String option3String = JOptionPane.showInputDialog("What would you like to do?\n" + 
    	     "1: Withdraw\n2: Deposit\n3: Check balance");
    	   int option3 = Integer.parseInt(option3String);
    	   int adj = JOptionPane.YES_OPTION;
    	   while (adj == JOptionPane.YES_OPTION) {
    	     switch (option3) {
    	       case 1: 
    		      String amount1String = JOptionPane.showInputDialog("How much to withdraw? ");
    		      double amount1 = Double.parseDouble(amount1String);
    		      acc.withdraw(amount1);
    		      JOptionPane.showMessageDialog(null, "Your balance is: " + acc.getBalance());
    		    case 2:
    		      String amount2String = JOptionPane.showInputDialog("How much to deposit? ");
    		      double amount2 = Double.parseDouble(amount2String);
    		      acc.deposit(amount2);
    		      JOptionPane.showMessageDialog(null, "Your balance is: " + acc.getBalance());
    		    case 3:
    		      JOptionPane.showMessageDialog(null, "Your balance is: " + acc.getBalance());
    	     }
    		  option3 = JOptionPane.showConfirmDialog(null, "Would you like to make more adjustments");
          }
    	 }
    	 else if (acc instanceof CheckingAccount) {
               System.out.println("test");
        }
      } 
    }

  6. #6
    Soulpole is offline Member
    Join Date
    Jan 2012
    Location
    East Coast
    Posts
    6
    Rep Power
    0

    Default Re: Calling Variables

    Is this still true if SavingsAccount is an extension of Account?

    Thanks,
    Nate

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Calling Variables

    Declare in the proper scope an Account variable, say named account, which can refer to a SavingsAccount or a CheckingAccount object. Then use this variable where needed.

    Incidentally you're still using the Adjustments class wrong by trying to call its constructor as if it were a method.

  8. #8
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Calling Variables

    If You want to reference instance of Adjustments class, than reference variable of type Adjustments or its superclasses should be declared, and assigned to instance of Adjustments;
    Say: Adjustements adj = new Adjustements(savings);

    And this is ok as long as savings extends Account.

Similar Threads

  1. help with variables please
    By arimaliz in forum New To Java
    Replies: 3
    Last Post: 03-22-2011, 04:30 PM
  2. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  3. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:08 PM
  4. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •