Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java and programming newbie needing help

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java and programming newbie needing help

    Hi all, I know this is something so basic that most won't even look at it but considering that my son is 14 yr old, new to programming and java, maybe a good soul can help him... and we will appreciate a lot.

    First, he is doing the famous bank account program that must have a class and a tester... so far so good but after executing the last call from the tester he needs to test the value of the variable opt and there is no way he can get it... here is his code so far and in the last line of the class BankAccount the variable Opt (in red) is not defined. What should he do to continue his program there?

    Sorry for the so basic question.... and thanks in advance

    Speed
    public class BankAccount1tester {
     
          public static void main(String [] args){
     
     
     
                BankAccount1 student1 = new BankAccount1();
     
                BankAccount1.setName();
     
                BankAccount1.setAccountNumber();
     
                BankAccount1.setInitialBalance();
     
                BankAccount1.setOptions();
     
     
     
                }
     
    }

    import javax.swing.*;
     
    public class BankAccount1 {
     
           public static String setName(){
     
               JFrame BankAccount = new JFrame();     
     
        BankAccount.setSize(400,400);       
     
        BankAccount.setTitle("Name");
     
        BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        String name = JOptionPane.showInputDialog("Enter your name to create an account");
     
        return name;
     
          }
     
     
     
        public static int setAccountNumber(){
     
     
     
          JFrame BankAccount = new JFrame();     
     
            BankAccount.setSize(400,400);       
     
            BankAccount.setTitle("AccNum");
     
            BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
     
            int AccountNum = Integer.parseInt((AccountNumber));
     
            return AccountNum;
     
     
     
        }
     
     
     
            public static int setInitialBalance(){
     
     
     
                JFrame BankAccount = new JFrame();     
     
                BankAccount.setSize(400,400);       
     
                BankAccount.setTitle("IntBalance");
     
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
                String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
     
                int InitBalance = Integer.parseInt((InitialBalance));
     
                return InitBalance;
     
          }
     
            public static int setOptions (){
     
     
     
                JFrame BankAccount = new JFrame();     
     
                BankAccount.setSize(400,400);       
     
                BankAccount.setTitle("Options");
     
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
                String Options = JOptionPane.showInputDialog("What would you like to do?: \n" +
     
                                                                                                  "Enter 1 to withdraw \n" + 
     
                                                                                                  "Enter 2 to deposit \n" +
     
                                                                                                  "Enter 3 to check current balance \n");
     
                int Opt = Integer.parseInt((Options));
     
                return Opt;
     
     
     
            }
     
     
     
         if ( Opt  == 1){
     
                           }
     
     
     
     
     
                }
     
            }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java and programming newbie needing help

    If you had Opt as a class variable and had a getOptionsMethod that returned the option, then you could use the if statement

    if (getOptions() == 1)
    ....


    Put an int called Opt in your constructor.

    Also, you need a constructor for BankAccount1 anyway as it looks like you're calling one.

    Once you do this.

    You have the local variable that was Opt become Opt2 and

    for setOptions

    Opt = Opt2;

    and for getOptions

    return Opt;

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    speedmaster (December 7th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Java and programming newbie needing help

    Still lost... we went one step further but still fighting with variable scope... now we are reading the name, bank account, initial balance and options but when Actions tries to output them we just get null...

    What should we do to output those values without losing them somewhere?

    Cheers and thanks for the previous answer....

    Speed
    public class BankAccount1tester {
     
          public static void main(String [] args){
     
     
     
                BankAccount1 Bank = new BankAccount1();
     
                BankAccount1.setName();
     
                BankAccount1.setAccountNumber();
     
                BankAccount1.setInitialBalance();
     
                BankAccount1.setOptions();
     
                BankAccount1.Action();
     
     
     
     
     
                }
     
    }

    import javax.swing.*;
     
     
     
    public class BankAccount1 {
     
     
     
          public static double blank;
     
          public static double InitialBalance;
     
          public static double currentBalance = InitialBalance;
     
          public static double deposit;
     
          public static double withdraw;
     
          public static String name;
     
          public static String accountNumber;
     
     
     
          public static String setName(){
     
     
     
          JFrame BankAccount = new JFrame();     
     
        BankAccount.setSize(400,400);       
     
        BankAccount.setTitle("Name");
     
        BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        String name = JOptionPane.showInputDialog("Enter your name to create an account");
     
        return name;
     
          }
     
     
     
          public static double setAccountNumber(){
     
     
     
          JFrame BankAccount = new JFrame();     
     
            BankAccount.setSize(400,400);       
     
            BankAccount.setTitle("AccNum");
     
            BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
     
            double accountNum = Integer.parseInt((AccountNumber));
     
            return accountNum;
     
     
     
        }
     
     
     
            public static double setInitialBalance(){
     
     
     
                JFrame BankAccount = new JFrame();     
     
                BankAccount.setSize(400,400);       
     
                BankAccount.setTitle("IntBalance");
     
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
                String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
     
                int initBalance = Integer.parseInt((InitialBalance));
     
                return initBalance;
     
          }
     
            public static double setOptions (){
     
     
     
                JFrame BankAccount = new JFrame();     
     
                BankAccount.setSize(400,400);       
     
                BankAccount.setTitle("Options");
     
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
                String Options = JOptionPane.showInputDialog("Hello " + name + ", your account number is " + accountNumber + "\n" +
     
                                                                                                    "What would you like to do?: \n" +
     
                                                                                                  "Enter 1 to withdraw \n" + 
     
                                                                                                  "Enter 2 to deposit \n" +
     
                                                                                                  "Enter 3 to check current balance \n");
     
                int Opt = Integer.parseInt((Options));
     
                return Opt;
     
     
     
            }
     
            public static double Action(){
     
     
     
                JFrame BankAccount = new JFrame();     
     
                BankAccount.setSize(400,400);       
     
                BankAccount.setTitle("Options");
     
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
                if (setOptions()  == 1){
     
                String Withdraw = JOptionPane.showInputDialog("Enter amount to withdraw: ");
     
                double withdraw = Integer.parseInt((Withdraw));
     
               return withdraw;
     
                }
     
                else if (setOptions() == 2){
     
                      String Deposit = JOptionPane.showInputDialog("Enter amount to deposit: ");
     
                      double deposit = Integer.parseInt((Deposit));
     
                return deposit;
     
                }
     
                else if (setOptions() == 3){
     
                       JOptionPane.showMessageDialog(BankAccount, "Your current balance is: \n" + name, "Current Balance", JOptionPane.INFORMATION_MESSAGE);
     
     
     
            }    
     
                return (Double) blank;
     
        }
     
     
     
                }

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java and programming newbie needing help

    return (Double) blank;

    blank is never initialized.

    Have a methods that calculate balance
    Also, have a variable, initialized to 0.00 in constructor called balance.
    public void withdraw()
    {
    double withdraw2l = Double.parseDouble(JOptionPane.showMessageDialog(null, "Enter amount to withdraw", "Making a widthdrawl", JOptionPane.INFORMATION_MESSAGE));
    if (balance -withdraw2l <0)
    {
    JOptionPane.showMessageDialog(null, "Cannot have negative balance", "Negative withdrawl", JOptionPane.ERROR_MESSAGE);
    balance = balance;
    }
    else
    balance = balance - widthdrawl2;
    }
     
    public void deposit()
    {
    double deposit2 = Double.parseDouble(JOptionPane.showMessageDialog(null, "Enter amount to deposit", "Depositing", JOptionPane.INFORMATION_MESSAGE));
    balance = balance + deposit2;
    }
     
    public double getBalance()
    {
    return balance;
    }
     
    Now for Option 1
     
    if (option ==1)
    {
    withdraw();
    }
     
    else if (option ==2)
    {
    deposit();
    }
     
    else if (option ==3)
    {
    JOptionPane.showMessageDialog(null, "Your depost is $" +getDeposit() + ".", "Your balance statement", JOptionPane.INFORMATION_MESSAGE);
    }
     
    else
    {
    JOptionPane.showMessageDialog(null, "Enter a 1,2, or 3.", "Invalid option", JOptionPane.ERROR_MESSAGE);
    }
    and change return type of setOptions to void if you can.

    If it must be double, make it return balance or something.
    Last edited by javapenguin; December 7th, 2010 at 11:06 PM.

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Java and programming newbie needing help

    Hello speedmaster, welcome to the forums.

    You cannot put the if statement there because it is not in a method.
    Take a look at this example:

    import javax.swing.*;
     
    public class BankAccount1 {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
    	   public static int Opt;
     
           public static String setName(){
               JFrame BankAccount = new JFrame();
               BankAccount.setSize(400,400);      
               BankAccount.setTitle("Name");
               BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               String name = JOptionPane.showInputDialog("Enter your name to create an account");
               return name;
           }
     
           public static int setAccountNumber(){
        	   JFrame BankAccount = new JFrame();    
        	   BankAccount.setSize(400,400);      
        	   BankAccount.setTitle("AccNum");
        	   BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	   String AccountNumber = JOptionPane.showInputDialog("Enter the desired account number:");
        	   int AccountNum = Integer.parseInt((AccountNumber));
        	   return AccountNum;
     
           }
     
            public static int setInitialBalance(){
     
            	JFrame BankAccount = new JFrame();    
                BankAccount.setSize(400,400);      
                BankAccount.setTitle("IntBalance");
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                String InitialBalance = JOptionPane.showInputDialog("Enter your initial balance:");
                int InitBalance = Integer.parseInt((InitialBalance));
                return InitBalance;
          }
     
            public static int setOptions (){
     
            	JFrame BankAccount = new JFrame();    
                BankAccount.setSize(400,400);      
                BankAccount.setTitle("Options");
                BankAccount.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                String Options = JOptionPane.showInputDialog("What would you like to do?: \n" +
                		"Enter 1 to withdraw \n" +
                		"Enter 2 to deposit \n" +
                		"Enter 3 to check current balance \n");
     
                Opt = Integer.parseInt((Options));
                return Opt;
            }   
     
            public static void ifstatement(){
     
            	if(Opt == 1){
            		System.out.println("Opt is 1");
            	}
     
            }
     
        }

    Does it help?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. [SOLVED] [Method] needing some help
    By Perplexing in forum Object Oriented Programming
    Replies: 4
    Last Post: December 2nd, 2010, 04:03 PM
  2. java newbie help..
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 30th, 2010, 04:54 AM
  3. Newbie to any type of Programming
    By kcljeremy in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2010, 07:27 AM
  4. Java newbie...Help required
    By muraduk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 12th, 2010, 03:15 PM
  5. Newbie Programming problem
    By Pingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 01:10 AM