View Full Version : New to Java
W-Unit
04-25-2006, 01:27 AM
Hey guys,
I'm brand new to Java and the world of OOP, and I just wrote my second program to allow you to set and retrieve a username, password, and age for users.
Here's the code.
public class Hello {
public User() {
}
public User(String username, String password, Int age) {
usrname = username;
usrpass = password;
usrage = age;
}
public void setUserName(String newusrname) {
usrname = newusrname;
}
public void setPassword(String newpass) {
usrpass = newpass;
}
public void setAge(Int newage) {
usrage = newage;
}
public String getUserName() {
return usrname;
}
public String getPassword() {
return usrpass;
}
public Int getAge() {
return usrage;
}
public static void main(String args[]) {
usr1 = new User();
usr1.setUserName("UserOne");
usr1.setPassword("PasswordOne");
usr1.setAge(1);
usr2 = new User("UserTwo", "PasswordTwo", 2);
System.out.println("User 1: " + usr1.getUserName + " " + usr1.getPassword + " " +usr1.getAge);
System.out.println("\n");
System.out.println("User 2: " + usr2.getUserName + " " + usr2.getPassword + " " +usr2.getAge);
}
}
...And here's the errors I get when I compile it...
C:\j2sdk1.4.2_11\bin>javac Hello.java
Hello.java:2: invalid method declaration; return type required
public User() {
^
Hello.java:4: invalid method declaration; return type required
public User(String username, String password, Int age) {
^
2 errors
...I'm pretty sure I understand what these errors mean (return type would be like public String getPassword(), right?), but I just want these ... definitions (I dont know the word yet, new to the vocabulary) of User() to just be used for constructors...I don't want them to return anything.
Thanks for any help you can provide.
Spookster
04-25-2006, 02:32 AM
When you do not need a method to return a value then you would declare it as void. In your case though you are trying to declare your constructor which will not have any return type. The problem you are running into is that your constructor must have the same name as the class.
W-Unit
04-25-2006, 03:36 AM
Alright, thanks. That took care of those errors...
But now I get tons of "cannot resolve symbol" errors, which I cannot make sense of at all.
Modified code
public class User {
public User() {
}
public User(String username, String password, Int age) {
usrname = username;
usrpass = password;
usrage = age;
}
public void setUserName(String newusrname) {
usrname = newusrname;
}
public void setPassword(String newpass) {
usrpass = newpass;
}
public void setAge(Int newage) {
usrage = newage;
}
public String getUserName() {
return usrname;
}
public String getPassword() {
return usrpass;
}
public Int getAge() {
return usrage;
}
public static void main(String args[]) {
usr1 = new User();
usr1.setUserName("UserOne");
usr1.setPassword("PasswordOne");
usr1.setAge(1);
usr2 = new User("UserTwo", "PasswordTwo", 2);
System.out.println("User 1: " + usr1.getUserName + " " + usr1.getPassword + " " + usr1.getAge);
System.out.println("\n");
System.out.println("User 2: " + usr2.getUserName + " " + usr2.getPassword + " " + usr2.getAge);
}
}
...And my errors
C:\j2sdk1.4.2_11\bin>javac User.java
User.java:4: cannot resolve symbol
symbol : class Int
location: class User
public User(String username, String password, Int age) {
^
User.java:15: cannot resolve symbol
symbol : class Int
location: class User
public void setAge(Int newage) {
^
User.java:24: cannot resolve symbol
symbol : class Int
location: class User
public Int getAge() {
^
User.java:5: cannot resolve symbol
symbol : variable usrname
location: class User
usrname = username;
^
User.java:6: cannot resolve symbol
symbol : variable usrpass
location: class User
usrpass = password;
^
User.java:7: cannot resolve symbol
symbol : variable usrage
location: class User
usrage = age;
^
User.java:10: cannot resolve symbol
symbol : variable usrname
location: class User
usrname = newusrname;
^
User.java:13: cannot resolve symbol
symbol : variable usrpass
location: class User
usrpass = newpass;
^
User.java:16: cannot resolve symbol
symbol : variable usrage
location: class User
usrage = newage;
^
User.java:19: cannot resolve symbol
symbol : variable usrname
location: class User
return usrname;
^
User.java:22: cannot resolve symbol
symbol : variable usrpass
location: class User
return usrpass;
^
User.java:25: cannot resolve symbol
symbol : variable usrage
location: class User
return usrage;
^
User.java:28: cannot resolve symbol
symbol : variable usr1
location: class User
usr1 = new User();
^
User.java:29: cannot resolve symbol
symbol : variable usr1
location: class User
usr1.setUserName("UserOne");
^
User.java:30: cannot resolve symbol
symbol : variable usr1
location: class User
usr1.setPassword("PasswordOne");
^
User.java:31: cannot resolve symbol
symbol : variable usr1
location: class User
usr1.setAge(1);
^
User.java:33: cannot resolve symbol
symbol : variable usr2
location: class User
usr2 = new User("UserTwo", "PasswordTwo", 2);
^
User.java:35: cannot resolve symbol
symbol : variable usr1
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " +usr1.getAge);
^
User.java:35: cannot resolve symbol
symbol : variable usr1
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " +usr1.getAge);
^
User.java:35: cannot resolve symbol
symbol : variable usr1
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " +usr1.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable usr2
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " +usr2.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable usr2
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " +usr2.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable usr2
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " +usr2.getAge);
^
23 errors
Thanks once again for any help :)
Spookster
04-25-2006, 03:58 AM
Let's see for starters Java is case sensitive. To declare an integer you need to declare it as int not Int.
Other than that most of your errors are due to the fact that you did not declare any of your local variables. You need to determine the scope that you need for your local variables and declare them in the appropriate locations in your class/methods/contructors.
W-Unit
04-25-2006, 04:17 AM
Hmm, I knew that Java was case-sensitive. I just assumed Int should be capitalized because String is; weird how they do that.
Alright, anyway, I think I know what the problem is now, but I still don't really know how to fix it.
I've got the errors down to just nine now, some of them occur on my return statements, the rest occur on periods, which I assume is related to the former problem.
Updated code...
public class User {
public User() {
}
public User(String username, String password, int age) {
String usrname = username;
String usrpass = password;
int usrage = age;
}
public void setUserName(String newusrname) {
String usrname = newusrname;
}
public void setPassword(String newpass) {
String usrpass = newpass;
}
public void setAge(int newage) {
int usrage = newage;
}
public String getUserName() {
return usrname;
}
public String getPassword() {
return usrpass;
}
public int getAge() {
return usrage;
}
public static void main(String args[]) {
User usr1 = new User();
usr1.setUserName("UserOne");
usr1.setPassword("PasswordOne");
usr1.setAge(1);
User usr2 = new User("UserTwo", "PasswordTwo", 2);
System.out.println("User 1: " + usr1.getUserName + " " + usr1.getPassword + " " + usr1.getAge);
System.out.println("\n");
System.out.println("User 2: " + usr2.getUserName + " " + usr2.getPassword + " " + usr2.getAge);
}
}
..And the errors.
C:\j2sdk1.4.2_11\bin>javac User.java
User.java:19: cannot resolve symbol
symbol : variable usrname
location: class User
return usrname;
^
User.java:22: cannot resolve symbol
symbol : variable usrpass
location: class User
return usrpass;
^
User.java:25: cannot resolve symbol
symbol : variable usrage
location: class User
return usrage;
^
User.java:35: cannot resolve symbol
symbol : variable getUserName
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " + usr1.getAge);
^
User.java:35: cannot resolve symbol
symbol : variable getPassword
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " + usr1.getAge);
^
User.java:35: cannot resolve symbol
symbol : variable getAge
location: class User
System.out.println("User 1: " + usr1.getUserName + " " + usr1.ge
tPassword + " " + usr1.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable getUserName
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " + usr2.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable getPassword
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " + usr2.getAge);
^
User.java:37: cannot resolve symbol
symbol : variable getAge
location: class User
System.out.println("User 2: " + usr2.getUserName + " " + usr2.ge
tPassword + " " + usr2.getAge);
^
9 errors
..Is this due to scoping? If so where would I declare the variables?
Spookster
04-25-2006, 04:50 AM
An integer is one of the 8 java primitive data types. A String is not. The String class is just an array of the char primitive data type. All primitives will start with a lower case letter whereas classes begin with an upper case letter.
As for scope you need to determine what scope you need your variables to be. They can be class variables in which case they can be accessed anywhere within the class. If you make them local variables that means they exist inside a method and are accessible anywhere in that method after they are declared and up to the point the method is closed with a curly brace. And then you have instance variables which are part of whatever object contains them.
In your case you probably want to delcare your variables as class variables so they have scope throughout your class. A class variable would normally be declared at the top of your class outside any of your methods/constructors and before they are first used.
TR-VandaL
04-25-2006, 07:08 AM
"User.java:XX: cannot resolve symbol
symbol : variable abcxyz"
It's saying that abcxyz is a variable because you aren't telling Java that it's actually a method/function. Add "()" after everything that is a method.
W-Unit
04-26-2006, 12:30 AM
Problem solved :D
Thanks everyone :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.