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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: "previously declared string"?... help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Question "previously declared string"?... help

    I do not know what "previously declared" means. I am in the process of completing a very simple short answer homework assignment. Short answer.. Write a line of code to print each of the following in the command prompt window:
    a. your name
    b. a previously declared String named lastName
    c. a blank line
    d. the name of your instructor tabbed 24 characters to the right.

    My response:

    a: System.out.println("Taylor Reichert");
    b: String = lastName();
    c: System.out.println();
    d: System.out.println("\t\t\tJane Doe");

    Just looking for a little guidance, general help understanding letter b. and the term "previously declared". Thank you in advance, Taylor.
    Last edited by Taylorleemusic; September 15th, 2010 at 05:56 PM.

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: "previously declared string"?... help

    ok, you seem to have a,c, and d correct (if the tabbing is right, I'm not sure on that). However, this line concerns me:
    String = lastName();

    Ok, Variables in Java (and all programming languages I believe) need 3 things:
    1) Type
    2) Variable Name
    3) Value

    For you, your Type is String. But, you havent given it a variable name.

    You specify the Variable Name immediately after declaring its type and immediately before the equals sign. You Variable Name cannot begin with any character other than either an uppercase or lowercase letter.

    So, if you wanted to create a String Variable and name it lastName, you would do this:
    //Creates an empty String with the Variable Name: lastName
    String lastName = "";

    In your code, by saying:
    You are saying there is a method called lastName(). Also, by making a String equal to that, you are saying that method returns a String. So, if you dont have a method in your code that looks like this:
    public String lastName()
    Then we have an issue.

    If you do not have a method for this, I would assume you are trying to separate the Last Name from the whole name. Provided the Last Name and First Name are only 1 word names, you can use String's handy split method, which can be found in the String API.

    The split method returns an array of Strings based on the delimiter you provided it. A delimiter is the indication as to where you want to split the String. For splitting the name, we want to use the delimiter of a space. So, we create a String array and use the split method to initialize our String array:
    //Variable that contains entire name
    String name = "Taylor Reichert";
    //Separate the Name
    String[] splitName = name.split(" ");

    Now we want to get the Last Name from that String array. We know the Last Name will be the word, but we cannot be sure if the middle name or whatever was included. So it is best to get the String that is last in the String array. To get that, we need to find the length of the array, then subtract 1 to get the index of the last String in the array. The length of an array is achieved by the saying array.length. Note how this is not a method. That is an Array thing, but don't worry yourself with that for now. Here is how we get the Last Name from the array:
    String lastName = splitName[splitName.length-1];

    Tell me if that helps. I feel I may have overshot the expectations of your assignment.
    Last edited by aussiemcgr; September 15th, 2010 at 07:22 PM.

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

    Taylorleemusic (September 15th, 2010)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,702 Times in 2,652 Posts

    Default Re: "previously declared string"?... help

    what "previously declared" means
    I would assume that phrase was normal English.
    Previously - done before now or before this line in the program.

  5. #4
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Default Re: "previously declared string"?... help

    Wow, that is exactly what I was looking for, very helpful. Aussiemcgrfor, thank you for taking the time explain in detail.
    Last edited by Taylorleemusic; September 15th, 2010 at 11:01 PM.

Similar Threads

  1. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  2. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM
  3. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM