"previously declared string"?... help
Hi,
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.
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:
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:
Code java:
//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:
Code java:
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:
Code java:
//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:
Code java:
String lastName = splitName[splitName.length-1];
Tell me if that helps. I feel I may have overshot the expectations of your assignment.
Re: "previously declared string"?... help
Quote:
what "previously declared" means
I would assume that phrase was normal English.
Previously - done before now or before this line in the program.
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.