problem understanding java assignment

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 793 Views - Last Post: 19 June 2012 - 02:44 PM Rate Topic: -----

#1 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

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.

Is This A Good Question/Topic? 0
  • +

Replies To: problem understanding java assignment

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

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
Was This Post Helpful? 2
  • +
  • -

#3 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 18 June 2012 - 06:59 PM

View Postpbl, 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


WOW.awesome u r super genious.thanks bud. Let me try this suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 18 June 2012 - 08:42 PM

View Postpbl, 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


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?
Was This Post Helpful? 0
  • +
  • -

#5 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

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
Was This Post Helpful? 0
  • +
  • -

#6 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 18 June 2012 - 09:24 PM

View Postnatecat, 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 = "";  
            }  
        }    

Was This Post Helpful? 0
  • +
  • -

#7 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

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(',');
Was This Post Helpful? 1
  • +
  • -

#8 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 18 June 2012 - 09:42 PM

View Postnatecat, on 18 June 2012 - 09:32 PM, said:

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(',');


ohhhh! :blush: Let me try this.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

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 = "";  
		}  


Was This Post Helpful? 1
  • +
  • -

#10 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 19 June 2012 - 07:54 AM

View Postpbl, 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 :bananaman: I just needed to make some minor changes to the code you gave me. That was a nice way to teach me. U gave me the code but with errors so I can learn by fixing it. thanks PBL.
Was This Post Helpful? 0
  • +
  • -

#11 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

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?
Was This Post Helpful? 0
  • +
  • -

#12 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: problem understanding java assignment

Posted 19 June 2012 - 09:26 AM

You have to import the class from the jar.
Was This Post Helpful? 0
  • +
  • -

#13 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 19 June 2012 - 09:54 AM

View Postnatecat, on 19 June 2012 - 09:26 AM, said:

You have to import the class from the jar.


Hey can u tell me step by step on how to import this correctly?
Was This Post Helpful? 0
  • +
  • -

#14 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

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>

Was This Post Helpful? 0
  • +
  • -

#15 rosepetalpowder  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 28-February 12

Re: problem understanding java assignment

Posted 19 June 2012 - 10:21 AM

View Postnatecat, on 19 June 2012 - 10:01 AM, said:

At the top of your document type this line:
import <package name>.<class name>


shud I have < > tags when I am writing package name and class name ? :stupid:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2