25 Replies - 477 Views - Last Post: 19 June 2012 - 02:44 PM
#1
problem understanding java assignment
Posted 18 June 2012 - 06:38 PM
This is my java question (this is an online course, prof is not much help)
In this lab, you are going to create a class called NameDecoder which will be able to read textual names and figure out the first, last and middle names based on how the name is formatted. The class will be able to recognize the following name formats:
First last
First middle last
Last, first
Last, first middle
Give me a clue on how to start this.
So far what I have done is Name class with get methods for first name,middle name, last name.
In this lab, you are going to create a class called NameDecoder which will be able to read textual names and figure out the first, last and middle names based on how the name is formatted. The class will be able to recognize the following name formats:
First last
First middle last
Last, first
Last, first middle
Give me a clue on how to start this.
So far what I have done is Name class with get methods for first name,middle name, last name.
Replies To: problem understanding java assignment
#2
Re: problem understanding java assignment
Posted 18 June 2012 - 06:47 PM
if you pass the complete String to the method (or constructor) you can
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
#3
Re: problem understanding java assignment
Posted 18 June 2012 - 06:59 PM
pbl, on 18 June 2012 - 06:47 PM, said:
if you pass the complete String to the method (or constructor) you can
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
WOW.awesome u r super genious.thanks bud. Let me try this suggestion.
#4
Re: problem understanding java assignment
Posted 18 June 2012 - 08:42 PM
pbl, on 18 June 2012 - 06:47 PM, said:
if you pass the complete String to the method (or constructor) you can
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
String[] token = str.split(" ");
if token.length == 2 you have First Last or Last First you can deifferentiate them if the last digit of token]0] is \,\ or not
if token.length == 3 you have First Middle Last or Last, First Middle. Again the last digit of token[0] will determine which pattern is used
Alright! I got the code this far.
Name1(String fullName_) {
fullName = fullName_;
maidenName = "";
String[] nameSplit = fullName.split(" ");
if (nameSplit.length == 3) {
firstName = nameSplit[0];
middleName = nameSplit[1];
lastName = nameSplit[2];
}
else {
firstName = nameSplit[0];
lastName = nameSplit[1];
middleName = "";
}
}
What did you mean by "Again the last digit of token[0] will determine which pattern is used" ?
As per your advise. I got it to differentiate fname, mname, lname but what if they type reverse order lname,fname mname?
#5
Re: problem understanding java assignment
Posted 18 June 2012 - 09:07 PM
Check for a comma. if the name is in reverse order there should be a comma
#6
Re: problem understanding java assignment
Posted 18 June 2012 - 09:24 PM
natecat, on 18 June 2012 - 09:07 PM, said:
Check for a comma. if the name is in reverse order there should be a comma
I did the following. It didnt work. Should I do namesplit2 = lastname.split(",")
shud i do a nested if else for legnth ==3?
Name1(String fullName_) {
fullName = fullName_;
maidenName = "";
String[] nameSplit = fullName.split(" ");
String[] nameSplit2 = fullName.split(",");
if (nameSplit.length == 3) {
firstName = nameSplit[0];
middleName = nameSplit[1];
lastName = nameSplit[2];
}
else if(nameSplit2.length == 3) {
lastName = nameSplit[0];
firstName = nameSplit[1];
middleName = nameSplit[2];
}
{
firstName = nameSplit[0];
lastName = nameSplit[1];
middleName = "";
}
}
#7
Re: problem understanding java assignment
Posted 18 June 2012 - 09:32 PM
No you should make a char array out of each of the strings in the array and then do this to each of the char arrays: Arrays.asList(char[] name here).contains(',');
#8
Re: problem understanding java assignment
Posted 18 June 2012 - 09:42 PM
#9
Re: problem understanding java assignment
Posted 19 June 2012 - 06:24 AM
It is more something like that... and you have to remove the , when it is there 
String[] nameSplit = fullName.split(" ");
char coma = nameSplit[0].charAt(nameSplit[0].length() - 1);
if(coma == ',')
nameSplit[0] = nameSplit[0].substring(0, nameSplit[0].length() - 1);
if (nameSplit.length == 3) {
if(coma == ',') {
lastName = nameSplit[0];
firstName = nameSplit[1];
middleName = nameSplit[2];
}
else {
firstName = nameSplit[0];
middleName = nameSplit[1];
lastName = nameSplit[2];
}
}
else {
if(coma == ',') {
lastName = nameSplit[0];
firstName = nameSplit[1];
}
else {
firstName = nameSplit[0];
lastName = nameSplit[1];
}
middleName = "";
}
#10
Re: problem understanding java assignment
Posted 19 June 2012 - 07:54 AM
pbl, on 19 June 2012 - 06:24 AM, said:
It is more something like that... and you have to remove the , when it is there 
String[] nameSplit = fullName.split(" ");
char coma = nameSplit[0].charAt(nameSplit[0].length() - 1);
if(coma == ',')
nameSplit[0] = nameSplit[0].substring(0, nameSplit[0].length() - 1);
if (nameSplit.length == 3) {
if(coma == ',') {
lastName = nameSplit[0];
firstName = nameSplit[1];
middleName = nameSplit[2];
}
else {
firstName = nameSplit[0];
middleName = nameSplit[1];
lastName = nameSplit[2];
}
}
else {
if(coma == ',') {
lastName = nameSplit[0];
firstName = nameSplit[1];
}
else {
firstName = nameSplit[0];
lastName = nameSplit[1];
}
middleName = "";
}
Yaaay! its working
#11
Re: problem understanding java assignment
Posted 19 June 2012 - 08:55 AM
ok Am back again to learn how to do JAR file - second part of assignment.
Create a project to build a jar file and build the NameDecoder class and store it in a jar file. Then, start a new project, import the classes from the jar file and write a Main class. Main should prompt the user to enter a series of names, terminated by end on the last line. It should use the NameDecoder to pick the name apart and print out the first, middle and last names, clearly labelled so we can tell which one is which.
What I have done so far -
create new project - java class library - NameDecoder class - click hammer to build jar file.
Then create new project - java application - Main class - click source packages - properties - libraries - add the jar file.
Now under Main I have the code but my main is saying namedecoder class is not there.
what other step am i missing?
Create a project to build a jar file and build the NameDecoder class and store it in a jar file. Then, start a new project, import the classes from the jar file and write a Main class. Main should prompt the user to enter a series of names, terminated by end on the last line. It should use the NameDecoder to pick the name apart and print out the first, middle and last names, clearly labelled so we can tell which one is which.
What I have done so far -
create new project - java class library - NameDecoder class - click hammer to build jar file.
Then create new project - java application - Main class - click source packages - properties - libraries - add the jar file.
Now under Main I have the code but my main is saying namedecoder class is not there.
what other step am i missing?
#12
Re: problem understanding java assignment
Posted 19 June 2012 - 09:26 AM
You have to import the class from the jar.
#13
Re: problem understanding java assignment
Posted 19 June 2012 - 09:54 AM
#14
Re: problem understanding java assignment
Posted 19 June 2012 - 10:01 AM
At the top of your document type this line:
import <package name>.<class name>
#15
Re: problem understanding java assignment
Posted 19 June 2012 - 10:21 AM
|
|

New Topic/Question
Reply



MultiQuote




|