Question:
Java Programming errors?
Media Blitz
2012-04-18 22:33:04 UTC
Ok, so basically I am making a banking program, the kind ATMs run on. This is part one of two parts:

public class Person{


private String fsym;
private String lsym;

public Person(String firstname, String lastname){
if (firstname == null || lastname == null){
throw new NullPointerException();
}

fsym = firstname;
lsym = lastname;
}

public String getFirstName(){
return fsym;
}

public String getLastName(){
return lsym;
}

public boolean equals (Object other){
if (this != other){
return false;
}
return true;
}

public String toString(){
return "fsym " + "lsym";
}
}

All of this compiles, but because there is no main method, it cannot run. So, I put the main method after public Person{ but after I attempt to compile it, I get a bunch of errors. Such as:

Person.java:5: error: illegal start of expression
private String fysm;

I do not want to write the rest, but they are more or less along the same line.
Three answers:
drail159
2012-04-18 22:46:32 UTC
You're right nothing should happen. I was just trying to get rid of the error before. If you do want some thing to happen you can use main to call your functions if the variables are declared in main. If you want to create instances of the class Person remove main from your program and make another file in the same folder which looks like this.



public class PersonTest

{



public static void main(String[] args)

{



Person person1 = new Person("Samuel", "Adams");

Person person2 = new Person("Kevin", "Smith");



System.out.println(person1.getFirstName());

System.out.println(person1.getLastName());

System.out.println(person1.toString());



System.out.println(person1.equals(person2));



}



}



The output should read:

Samuel

Adams

Samuel Adams

false



If you like I can send you a copy of my files via email.

There, you should be all set now.
Saksham
2012-04-19 05:59:44 UTC
I think error is because main function should be in beginning then i think it will be okay
codrguy
2012-04-19 05:43:31 UTC
you are either missing curly brackets or have them in the wrong order. make sure main is outside of any method and has it's closing curly bracket.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...