Results 1 to 6 of 6
Thread: Initials (what am i doing wrong)
- 05-10-2010, 08:48 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
Initials (what am i doing wrong)
public class Name {
private String firstName;
private String middleName;
private String lastName;
private String initials;
Name() {
firstName = null;
middleName = null;
lastName = null;
}
Name(String newFirstName, String newMiddleName, String newLastName){
firstName = newFirstName;
middleName = newMiddleName;
lastName = newLastName;
}
public void setFirstName(String newFirstName){
firstName = newFirstName;
}
public void setMiddleName(String newMiddleName){
middleName = newMiddleName;
}
public void setLastName(String newLastName){
lastName = newLastName;
}
public String getFirstName(){
return firstName;
}
public String getMiddleName(){
return middleName;
}
public String getLastName(){
return lastName;
}
public void concat(){
String fullName=firstName.concat(middleName).co…
int lengthName=fullName.length();
System.out.println("The length in characters of " + firstName + " " +middleName+ " " + lastName + " is: " + lengthName);
}
public void substring(){
initials = firstName.substring(0) + middleName.substring(0) + lastName.substring(0);
System.out.println(initials);
}
public void lastNameFirst(){
String s = lastName.concat(", ").concat(firstName).concat(" "+middleName.substring(0));
System.out.println(s);
}
public void fullNameInitials(){
String f = firstName.concat(" " +middleName).concat(" "+lastName+" ") + initials;
System.out.println(f);
}
}
public class NewNamesList {
public static void main(String[] args){
Name[] namesList = new Name[2];
namesList[0] = new Name("Superman", "bin", "batman");
namesList[1] = new Name("Ironman", "bin", "flash");
namesList[0].concat();
namesList[0].substring();
namesList[0].lastNameFirst();
namesList[0].fullNameInitials();
namesList[1].concat();
namesList[1].substring();
namesList[1].lastNameFirst();
namesList[1].fullNameInitials();
}
}
this is what the teacher said
- 05-10-2010, 08:49 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
public String initials()—returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter—then you can upcase this one-letter string.)
- 05-10-2010, 09:03 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Well, that is kinda wierd coming from a teacher, since there is a:
method, so I don't see how a one character string would be any worse for wear then an acctual character from a charAt() method.Java Code:Character.toUpperCase('a');Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-10-2010, 10:28 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
public char charAt(int index){
String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);
System.out.println(intials);
}
i can use that, but it's not compiling for me.
- 05-10-2010, 11:06 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
No, you missed the point completely. Use the charAt() function to get the first letters of all 3 Strings, convert them to upper case with the Character.toUpperCase() method and use them to create another String, and return the newly formed String. charAt() is a method from the String class, I was saying that you can use that instead of substring. And seeing the charAt() method you've written up, I'd suggest to go back and start from the beginning, what are variables, what are methods, what are method arguments, what are methods supposed to return etc.
Java Code:public char charAt(int index){ //what purpouse does the index serve? //why return //a char? String initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0); System.out.println(intials); //you defined the methods return type to be char. Where is the return statement? //and the return type should definately not be char, think about what this method is supposed to do }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-10-2010, 11:08 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
what's wrong here ?
By doha786 in forum New To JavaReplies: 6Last Post: 03-26-2010, 12:21 PM -
what am i doing wrong here?
By GPB in forum New To JavaReplies: 3Last Post: 03-21-2010, 04:04 PM -
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
What am I doing wrong??
By NoNickName in forum New To JavaReplies: 3Last Post: 04-23-2009, 11:04 PM -
how do i initials the word so i get the first letter only
By mikeitalydz in forum New To JavaReplies: 15Last Post: 04-19-2009, 10:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks