Trying to learn and I'm stuck
Hey everyone, I am very new to Java programming and I'm stuck!
I'm working through a book that sets me exercises etc but I wondered if you could tell me what I'm doing wrong here.
Im trying to add a conditional statement to my constructor, that counts the amount of characters in a string and prints an error message if the string has less than 4 characters.
I know the String class has a length accessor method, and thats what I'm trying to use here.
Code :
public class Student
{
// the student's full name
private String name;
// the student ID
private String id;
// the amount of credits for study taken so far
private int credits;
/**
* Create a new student with a given name and ID number.
*/
public Student(String fullName, String studentID)
{
name = fullName;
id = studentID;
credits = 0;
if(studentID.length < 4)
{
System.out.println("You need 4 or more charaters");
}
}
Now this just doesn't compile, it has a problem with .length in my if statement.
It's not obvious why this won't work, could someone please help me out? :o
Thanks!
Re: Trying to learn and I'm stuck
Length is a method, not a field. the correct way to get the length of studentID is studentID.length() not studentID.length
You can look at the String documentation here: String (Java Platform SE 6)
Re: Trying to learn and I'm stuck
HA! thanks.. I was just being dumb then.
:P