need help
I am having trouble getting my testName program to run the way I want it to. I can't get the names to run with the name it supose to. This is what I get.
Please enter full name
tanesha nicole mcclain
Please enter full name2
clarence howard mcclain
clarence
howard
mcclain
first-middle-last version : tanesha nicole mcclain
last-first-middle version : mcclain, tanesha nicole
initials = T.N.M.
length = 20
first-middle-last version : clarence howard mcclain
last-first-middle version : mcclain, clarence howard
initials = C.H.M.
length = 21
They are not equal
publicclass TestName{
publicstaticvoid main (String[] args){
String first;
String middle;
String last;
Scanner scan =new Scanner(System.in);
System.out.println("Please enter full name");
first= scan.next();
middle= scan.next();
last= scan.next();
Name n1 =new Name(first, middle, last);
System.out.println("Please enter full name2");
first= scan.next();
middle= scan.next();
last= scan.next();
Name n2 =new Name(first, middle, last);
System.out.println();
System.out.println(first);
System.out.println(middle);
System.out.println(last);
System.out.println("first-middle-last version : " + n1.getFirstMiddleLast());
System.out.println("last-first-middle version : " + n1.getLastFirstMiddle());
System.out.println("initials = " + n1.getInitials().toUpperCase());
System.out.println("length = " + n1.getLength());
System.out.println();
System.out.println("first-middle-last version : " + n2.getFirstMiddleLast());
System.out.println("last-first-middle version : " + n2.getLastFirstMiddle());
System.out.println("initials = " + n2.getInitials().toUpperCase());
System.out.println("length = " + n2.getLength());
if(n1.equals(n2))
System.out.println("They are equal");
else
System.out.println("They are not equal");
}
}
[2827 byte] By [
tmlucky13a] at [2007-11-27 1:06:50]

Tanesha, what is wrong w/ the output, specifically?
When I enter the first full name I get this. It does not print my first, middle, and last name. It print the name2 instead
clarence
howard
mcclain
first-middle-last version : tanesha nicole mcclain
last-first-middle version : mcclain, tanesha nicole
initials = T.N.M.
length = 20
When I enter the second full name it does not print first, middle, and last name of name2, but it prints the other stuff.
first-middle-last version : clarence howard mcclain
last-first-middle version : mcclain, clarence howard
initials = C.H.M.
length = 21
They are not equal
> When I enter the first full name I get this. It does
> not print my first, middle, and last name. It print
> the name2 instead
>
> clarence
> howard
> mcclain
> first-middle-last version : tanesha nicole mcclain
> last-first-middle version : mcclain, tanesha nicole
> initials = T.N.M.
> length = 20
>
> When I enter the second full name it does not print
> first, middle, and last name of name2, but it prints
> the other stuff.
>
> first-middle-last version : clarence howard mcclain
> last-first-middle version : mcclain, clarence howard
> initials = C.H.M.
> length = 21
>
> They are not equal
It will only do what you tell it to do. If you want it to print each name as you enter them, then you have to have a System.out.println (SOP) there.
For instance, if you want it to print name 1 after entering name 1, and likewise for name 2, then have a set of System.out.println methods after that name is entered:
Instead of this:
Name n1 = new TestName().new Name(first, middle, last);
System.out.println("Please enter full name2");
first = scan.next();
middle = scan.next();
last = scan.next();
Name n2 = new TestName().new Name(first, middle, last);
System.out.println();
System.out.println(first);
System.out.println(middle);
System.out.println(last);
put in some more System.out.println's like so:
Name n1 = new TestName().new Name(first, middle, last);
System.out.println();
System.out.println(first); // these have been added to print name1
System.out.println(middle); // these have been added to print name1
System.out.println(last); // these have been added to print name1
System.out.println("Please enter full name2");
first = scan.next();
middle = scan.next();
last = scan.next();
Name n2 = new TestName().new Name(first, middle, last);
System.out.println();
System.out.println(first);
System.out.println(middle);
System.out.println(last);
If you need it to print both names AFTER you have entered both, then you will have to use calls to the Name class getters (if they have them).i.e. System.out.println(n1.getFirst); etc...
Thanks I have the name part working. One more thing. It tell that the names are not equal, but I can get it to tell me if the names are equal. I don't know if my if statement is wrong are in the wrong place. can you help me.
> Thanks I have the name part working. One more thing.
> It tell that the names are not equal, but I can get
> it to tell me if the names are equal. I don't know
> if my if statement is wrong are in the wrong place.
> can you help me.
Your if and equals statements look fine to me. I don't know, however, how the Name class implements the equals function and that would be critical to know before saying everything is alright.
One other thing, as a rule I like to enclose all of my if and else statement blocks in curly braces, even if it only has one statement. This saves me from a world of trouble later on as it prevents me from the following bug:
Say I have this code:
if (a == b)
doThis();
Then later I decide that I want to have one more method called if a ==b and I code this:
if (a == b)
doThis();
doThat();
This won't run as I expected it would. doThat() always be called regardless if a == b or not. Better to use braces from the get-go:
if (a == b)
{
doThis();
}
Then when I if want to add a second method call, it will work as desired:
if (a == b)
{
doThis();
doThat();
}
You are comparing two objects of your Name class. If you have not written your own equals method then it will use the equals method it inherits from Object. Have you written an equals method in the Name class?