Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create a Name class to manage different name changes of a person.

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I have gotten this far in writing my Name class:


And I'm starting to test what I have so far. When I enter my full name though, I get:
First name: null


Can anyone help me figure out what I'm doing wrong? Thanks! Y'all are the best!

Name(): Initializes full and first names to “Anonymous” and middle, last
and maiden names to empty string.
Name(String fullName): Initializes full name to fullName and initializes
first, middle and last names appropriately. If only 2 names are in the
fullName, middle name is set to empty string. For all cases, maiden name
is set to empty string.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you have your assignments reversed: nameSplit[0] = firstName;


rather than firstname=nameSplit[0];



 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Rainey wrote:It looks like you have your assignments reversed: nameSplit[0] = firstName;


rather than firstname=nameSplit[0];




Oh yeah, how can you miss that statement? Nice catch.
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gahh. I didn't even notice that. Thanks!!
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I've made some changes to my code. And now I'm having a problem getting the full name to print out correctly. For example: Nicolas Patric Young should print out as Nicolas P. Young


 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see where you actually call the getFullName() method anywhere....
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. I feel really dumb. Haha, thanks for pointing that out. Now I'm almost done with everything. I'm writing my test class and am having issues compiling because of the methods and for loops. It's probably another really stupid mistake but I just can't figure it out.
If you guys could help me with this I would appreciate it so much! Y'all have helped me out so much already!



Thank you!!!>
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kd Martin wrote: Now I'm almost done with everything. I'm writing my test class and am having issues compiling because of the methods and for loops. It's probably another really stupid mistake but I just can't figure it out. Thank you!!!>



That's a pretty vague description of the problem.....
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Sorry, I forgot to paste that. Here it is:

You will create a Name class to manage different name
changes of a person.

Test Program
1. Have the user enter the number of names (=x).
2. Have the user enter in x full names (e.g. Hillary Diane Rodham, Barrack Obama).
3. While (The user hasn’t typed in “exit” or “Exit”)
Have the user type in a person’s first name
Ask which name they would like to change (first, middle or last)
Ask the user what they would like to change that name to
If they want to change last name, ask the user if they would like to change maiden name also.
If yes, current last name becomes maiden name, and new last name becomes last name.
4. Print everybody’s name in the specified format:


Print format:
Full Name: Hillary D. Clinton (Note, only middle initial is printed)
First Name: Hillary
Middle Name: Diane
Last Name: Clinton
Maiden Name: Rodham

 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll rephrase it a bit...

What's not working?


I think the general atmosphere here is to lend a helping hand understanding concepts.... not to do the assignment for you.

 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a bunch of compiling errors all pertaining to the methods and the for loops.
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: ';' expected
public static boolean cont(String response) {
^
TestName.java:31: ')' expected
public static boolean cont(String response) {
^
TestName.java:31: illegal start of expression
public static boolean cont(String response) {
^
TestName.java:31: ';' expected
public static boolean cont(String response) {
^
TestName.java:71: illegal start of type
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ')' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: illegal start of type
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ';' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: > expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: '(' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: illegal start of type
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:71: ><identifier> expected
for (int i=0; i<nameArray.Length; i++) {
^
TestName.java:71: ';' expected
for (int i=0; i><nameArray.Length; i++) {
^
TestName.java:72: illegal start of type
nameArray[i].displayNames();
^
TestName.java:72: ';' expected
nameArray[i].displayNames();
^
TestName.java:72: invalid method declaration; return type required
nameArray[i].displayNames();
^
TestName.java:74: class, interface, or enum expected
}
^
21 errors
>
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know its probably just a couple issues that are causing these problems, but I just can't figure out what I'm doing wrong when I create the methods in the TestName class. I'm thinking it has something to do with the array of objects but I'm just not sure what.
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically when you see a whole bunch of syntax related errors such as missing semi-colon, parens, etc, the series of errors spawns from one syntax error. If you start at the top of the error listing, verify matching braces and parens, properly placed semi-colons,etc. The errors give you a clue what to look for (usually) and the line number of the file where to start looking.


Start here:

You don't have an array called "names"
The size of an array is found with .length, not .Length
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing a curly brace which makes the following methods look like they are inside the main method.

Define the 'response' variable outside the do-loop. (variable scoping)

Do arrays have a startsWith() method?



You'll have some other issues to sort out once you fix those. Read the error messages. They are typically useful in figuring out where to start looking for the problems.
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixing that for loop didn't change any of the errors :/ I think my way of searching through the array of strings is incorrect. Could that be part of the issue I'm having compiling?
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! I didn't see that. Thank you
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I have now


And now I only have 2 errors.

TestName.java:29: cannot find symbol
symbol : variable response
location: class TestName
} while (cont(response));
^
TestName.java:51: cannot find symbol
symbol : method startswith(java.lang.String)
location: class Name
boolean b = nameArray[i].startswith(first_);

For the do while loop, I want it to continue if the user's response is not the word "Exit". I created a boolean method for that purpose, but I don't see how it cannot find the symbol. I also don't understand how a symbol is not found for the startswith method in line 51. I thought I defined all the variables :/>
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned the problem with 'response' in my previous post.

As for 'startswith', remember that everything in Java is case sensitive.
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I fixed response, but changing startswith to startsWith didn't fix the problem :/
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put it as a question before: does the startWith() method exist for arrays?

(if it doesn't exist, you can't call it, right?)
 
Kd Martin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out! Thank you so much for your help!
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations! That gives a nice sense of accomplishment, right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic