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 3 of 3

Thread: Trying to learn and I'm stuck

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

    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?

    Thanks!


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default 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)

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

    knightknight (June 1st, 2011)

  4. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Trying to learn and I'm stuck

    HA! thanks.. I was just being dumb then.


Similar Threads

  1. Best Book to learn Java
    By ShadowKing98 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 1st, 2013, 06:57 AM
  2. New to this..so lost trying to learn...
    By ryan1234 in forum Object Oriented Programming
    Replies: 5
    Last Post: September 21st, 2010, 05:35 PM
  3. How long did it take you to learn?
    By JavaLearner in forum The Cafe
    Replies: 5
    Last Post: March 24th, 2010, 04:42 PM
  4. here to learn and grow
    By cejay in forum Member Introductions
    Replies: 2
    Last Post: March 1st, 2010, 03:04 AM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM