Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2009, 09:55 PM   PM User | #1
kcc989
New Coder

 
Join Date: Dec 2009
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
kcc989 is an unknown quantity at this point
Making nouns plural

I have an assignment for a class that I am not sure how to approach. A string, a noun, is input and the program is supposed to make it plural based on the ending. If it ends in a double consonant then I add one ending, if it ends in a vowel I add a different one and then if it ends in two vowels or two consonants I add a third, different ending. I am supposed to do this using string methods. For the project the vowels are A, C, L, and S. I have to read the data in using the string tokenizer.

I am not sure how to approach making the loops I need to use to make the words plural. Can anyone help??

EDIT: I have to use if statements to do this.

Last edited by kcc989; 12-13-2009 at 11:12 PM..
kcc989 is offline   Reply With Quote
Old 12-13-2009, 10:21 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
String Tokenizer indicates to me that this is supposed to be Java, not Javascript. I'll request this to be moved to the Java forum.
You don't really need to do much by way of looping in this assignment aside from the token retrieving. All you need to do to loop a string tokenizer is to check if st.hasMoreTokens(). While thats true, it will loop. Retrieve each with nextToken().
Inside of this loop you simply want to check for the last chars. Work it from shortest to longest case, or use a substring to pull out the last few chars. Its up to you to handle those.

That help? Can't give too much information since this is an assignment and its considered plagiarism if you just get the code from here.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 12-13-2009, 10:22 PM   PM User | #3
blitz104
New Coder

 
Join Date: Sep 2008
Posts: 48
Thanks: 7
Thanked 0 Times in 0 Posts
blitz104 is an unknown quantity at this point
Based on the ending, you need to add a certain ending to the variable? I'd do it like this (where ip is the input):
Code:
var split = ip.split("")
//splits the word into its individual letters
var ending = split[ip.length-3] + split[ip.length-2] + split[ip.length-1]
//ending will contain the last 3 letters of the given noun
After that code, you can just have a series of if statements to check for each type of ending, and pluralize them accordingly
blitz104 is offline   Reply With Quote
Old 12-13-2009, 10:26 PM   PM User | #4
kcc989
New Coder

 
Join Date: Dec 2009
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
kcc989 is an unknown quantity at this point
Quote:
Originally Posted by blitz104 View Post
Based on the ending, you need to add a certain ending to the variable? I'd do it like this (where ip is the input):
Code:
var split = ip.split("")
//splits the word into its individual letters
var ending = split[ip.length-3] + split[ip.length-2] + split[ip.length-1]
//ending will contain the last 3 letters of the given noun
After that code, you can just have a series of if statements to check for each type of ending, and pluralize them accordingly
I can't split the code into individual letters because in my class we haven't covered that yet. Anyway I am struggling on using the statements to check for endings. This is the loop I am using right now, that isn't working.

Code:
public static void makePlural()
  	{
  		for (int i = 3; i < word.length();) {
        char c = word.charAt(i);
        if (c=='A' || c=='C' || c=='L' || c=='S') {output =
        	word + "G" ;}
        else {output = word + "GH";
        }
                }
  	}
And yes I put this in the wrong forum, my bad.
kcc989 is offline   Reply With Quote
Old 12-13-2009, 10:40 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
blitz's code is for Javascript, to split them.
Using charAt(x) would be my suggestion over substring simply since you don't need to keep the string - may as well save the memory.
The loop itself is a problem since you're not incrementing it anywhere, so it will infinitely loop.
This loop would start at the third fourth character in the string, and search from there. Thats not what you want, what you want is to search from the end of the loop. You can do this without the loop:
PHP Code:

int i 
word.length() - 1;
switch(
word.charAt(i))
{
    case 
'A':
    case 
'C':
    case 
'L':
    case 
'S':
        
output word "G";
        break;
    default:
        
output word "GH";

This is untested, so I have no idea if it will work or not. The switch may not actually be the best solution, since it sounds like you'll need to do extra testing.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 12-13-2009, 10:55 PM   PM User | #6
kcc989
New Coder

 
Join Date: Dec 2009
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
kcc989 is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
The loop itself is a problem since you're not incrementing it anywhere, so it will infinitely loop.
My teacher asked us to use loops to do it, so how would I incriminate it? Should I add a count to the loop and limit the number of times it loops?

My whole problem is I can't wrap my mind around how to correctly do this with a loop. All other approaches I can find seem to do it with topics I haven't covered yet and therefore can't use in my assingment.
kcc989 is offline   Reply With Quote
Old 12-13-2009, 11:08 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
In a for loop you need to control it somehow. Generally, you'd see something like for(int i = 0; i < count; ++i), where the final parameter is the post evaluation condition (the increment). You could ignore that and increment i within the loop itself, but you need to do something to control it or it will infinitely loop. You could potentially use infinite loops for sockets and stuff where you need to listen indefinitely. Those would be controlled with:
Code:
for (;;)
{
    // Do stuff.  If error, cause 'break'
}
// or
while(true)
{
    // do stuff.  If error, cause 'break'
}
I can see the loop with the string tokenizer, but the only other potential loop I can see is if a condition is unsatisfied.
Code:
boolean isSatisfied = false;
while (!isSatisfied)
{
    // Get the last character, evaluate it.  If not satisfied, check the previous character,
    // and so forth until satisfied is true.
}
So if you need to check up to the last three characters, you can do that with a loop:
Code:
boolean isSatisfied = false;
int iWordLength = word.length();
for (int i = iWordLength - 1; (i > iWordLength - 3) && !isSatisfied; --i)
{
    // Now, we start at the last character.  We will need to branch out from this point
    // Once the determination is made, set isSatisfied to true
}
There are several approaches you can take. The loop itself has little weight compared to the conditional checks you'll need to make to make a determination.

Aside from that, I also cannot see how you're expected to use loops on this.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 12-13-2009, 11:21 PM   PM User | #8
kcc989
New Coder

 
Join Date: Dec 2009
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
kcc989 is an unknown quantity at this point
Well the loop was involved with the string tokenizer part, but I just figured that part out, I forgot to define a method earlier in the code.

Anyway I was being ignorant before, I need to use if statements. There doesn't need to be a loop, I just need to use if statements. I may have just figured this out, but an I'd love to see an example someone using nested if statements to do this.
kcc989 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:54 AM.


Advertisement
Log in to turn off these ads.