0

Extremely simple question from an extremely simple high-school java student.

I've gathered user input and created an object "AccountOne". I must pass AccountOne into class printcode and use the printaccount method to print "myaccount.name, myaccount.address, myaccount.type, myaccount.balance". My teacher has been trying to explain inheritance and she's just losing me more and more with each day. From what I understood, object myaccount of type AccountOne should now "have" myaccount.name, myaccount.address, myaccount.type, myaccount.balance" within itself. And because I passed the object into the method, the method inherits whatever is in the object. Ergo, method printaccount should be able to find symbols "myaccount, my account.name, myaccount.address, myaccount.type, myaccount.balance". I've researched and everything doesn't make sense to me, it all appears to be about way more advanced stuff that I haven't even begun to grasp yet. If there's a duplicate question please point me to it.

When compiling i get 10+ errors saying "not a statement" or "cannot find symbol myaccount". At this point i'll just take the zero, I'm beyond frustrated. It's most likely something stupidly simple that i keep glancing over.

I've tried defining the variables within printcode before the main statement. Defined variables in AccountOne. Attempted to research different ways of define a method and couldn't find anything that made sense to me.

import java.util.Scanner;
public class AccountOne
{
 public String name;
 public String address;
 public String type;
 public double balance;
 
  public static void main(String args[])
  {
  Scanner keyboard = new Scanner(System.in);

  System.out.print("Enter Name ");
  String name=keyboard.nextLine();
  System.out.println();

  System.out.print("Enter Address ");
  String address=keyboard.nextLine();
  System.out.println();

  System.out.print("Enter type ");
  String type=keyboard.nextLine();
  System.out.println();

  System.out.print("Enter Balance ");
  double balance=keyboard.nextDouble();
  System.out.println();

  AccountOne myaccount=new AccountOne();
   myaccount.name=name;
   myaccount.address=address;
   myaccount.type=type;
   myaccount.balance=balance;

  printcode.printaccount(myaccount);
 }
}

Second Class - printcode:

public class printcode
{
    public static void main(String args[])
    {
      public static printaccount(AccountOne myaccount)
        {
            System.out.println("My Account = " + myaccount.name + " " + myaccount.address + " " + myaccount.type + " $" + myaccount.balance);
        }   
    }

}
Met Frust
  • 3
  • 4
  • 2
    I did remove the javascript tag as this is obviously about Java... – home Oct 12 '14 at 13:46
  • 2
    `main` method of which class is the starting point? – Tirath Oct 12 '14 at 13:49
  • I'm sorry, confused by your question. If by starting point you mean which class is the starting point I'm going to say class AccountOne is that starting point. I'm passing AccountOne into printaccount method in class printcode. All I need printaccount to do is print out my variables gathered from the user. – Met Frust Oct 12 '14 at 13:57
  • See your question. Its being edited again. You don't need 2 `main` there. Add the stack trace of error(s) if possible. – Tirath Oct 12 '14 at 14:00
  • Removed second main statement from class printcode. Very sorry, but I don't know what a stack trace is. I have the list of errors within my command prompt and quickly added those but it just turns into a massive block of text with no line breaks or spacing so I removed it. – Met Frust Oct 12 '14 at 14:19
  • why do you have two `main` methods? – EpicPandaForce Oct 12 '14 at 14:22
  • @MetFrust http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Tirath Oct 12 '14 at 14:40

2 Answers2

1

First, when we say - myaccount.name it means myaccount is an instance of MyAccount class and it has a member variable defined and which is name

For example -

class MyAccount {
    String name;
    public static void main (String...a) {
        //here am allowed to say myaccount.name
        MyAccount myaccount = new MyAccount();
    }
}

Second, the statement

System.out.println("My Account = " + myaccount.name " " + myaccount.address " " + myaccount.type " $" + myaccount.balance);

is all wrong. Modify it to

System.out.println("My Account = " + myaccount.name + " " + myaccount.address + " " + myaccount.type + " \\$" + myaccount.balance);

Now, you should get another exception which should tell you non-static variable address cannot be referenced from a static context. It means if you want to access name in the context of main which is static - you need to change the declaration of name to

static String name;

Third, we can have only one public class in file. Change public class printcode to class printcode. Need not to consider this if both classes are in different files.

Hope this helps. I copy pasted both the classes in one file.

Tirath
  • 2,294
  • 18
  • 27
  • Fixed print statement. So just to be sure I got this right, when I key _AccountOne myaccount=new AccountOne();_ this means I have a class called myaccount that contains a variable called name? As my teacher explained it, I thought I was creating an "object" called myaccount that could be passed to a method. Or are classes and objects the same thing? – Met Frust Oct 12 '14 at 14:57
  • You have a class `AccountOne` and `myaccount` is an instance of that class. Now, if you want to access `name` member variable, you would use this `myaccount` instance to access it if it is not qualified as `static` field. Incase `name` is `static` you should use `ClassName.fieldName` syntax to access it outside a non-static context. – Tirath Oct 12 '14 at 15:06
  • Yes, `AccountOne myaccount=new AccountOne();` means you are creating an object of `AccountOne` class called `myaccount`. – Tirath Oct 12 '14 at 15:11
  • I finally got the program running. Do I delete this question? – Met Frust Oct 12 '14 at 16:32
  • @MetFrust you don't have to delete it. You can accept one of the answers if you feel that helped you.By doing that you gain 2 points and the accepted answer get 15 points. – Tirath Oct 12 '14 at 16:33
  • Thanks so much for the help~ really appreciate it. – Met Frust Oct 12 '14 at 16:47
0

You have defined static method printaccount (in printcode class) within main method.Move it out from main method and should work.

In addition to above, there are couple of more changes as below

  1. You are missing fields like name/address etc (although i wont advice using public field instead use get/set methods)
  2. your System.out.println was having error wherein for concatenatign string "+" was missing.

    import java.util.Scanner;
    public class AccountOne
    {
    public String name;
    public String address;
    public String type;
    public double balance;
    public static void main(String args[])
    {
       Scanner keyboard = new Scanner(System.in);
    
       System.out.print("Enter Name ");
       String name=keyboard.nextLine();
       System.out.println();
    
       System.out.print("Enter Address ");
       String address=keyboard.nextLine();
       System.out.println();
    
       System.out.print("Enter type ");
       String type=keyboard.nextLine();
       System.out.println();
    
       System.out.print("Enter Balance ");
       double balance=keyboard.nextDouble();
       System.out.println();
    
       AccountOne myaccount=new AccountOne();
        myaccount.name=name;
        myaccount.address=address;
        myaccount.type=type;
        myaccount.balance=balance;
    
    printcode.printaccount(myaccount);
    }
    }
    
    public class printcode
    {
    
        public static void printaccount(AccountOne myaccount)
        {
            System.out.println("My Account = " + myaccount.name + " " + myaccount.address +  " " + myaccount.type + " $" + myaccount.balance);
        }   
    
    }
    
SMA
  • 36,381
  • 8
  • 49
  • 73