aspose file tools*
The moose likes Beginning Java and the fly likes Programming project... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Programming project..." Watch "Programming project..." New topic
Author

Programming project...

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Maybe you guys can help me with this one too...
I have the first part of it working, but I cant seem to figure out how to make just the exclamation part work, and I cant figure out how to make just the last part of it work either. Any suggestions? Id appreciate any help.

//**************************************************************
//* This program will ask a user to input a sentence, then *
//* if the sentence ends with an even number of characters *
//* and a question mark, it will output the word 'Yes'. *
//* If the sentence ends with a question mark and it is an *
//* odd number of characters, it will output the word 'No'. *
//* If the sentence ends with an exclamation mark, the output *
//* will say 'Wow!'. In all other cases, it will output *
//*'You always say that.' *
//**************************************************************
//* Author: Rose Evans *
//* Date last modified: October 14th 2004. *
//**************************************************************
public class OneLineSentence
{
public static void main(String[] args)
{

String sentence;


System.out.println("Please enter any sentence of your choice.");
sentence = SavitchIn.readLine();
sentence.length();

System.out.println("The length of the sentence is: "+ sentence.length());
{
if (sentence.length() % 2 == 0)


{
System.out.println("Yes");
}

else
{

System.out.println("No");

}
{
if

sentence.lastIndexOf("!");


{
System.out.println("Wow");
}



System.out.println("You always say, " + sentence);

}


}


}


) ) ) )
rahul V kumar
Ranch Hand

Joined: May 20, 2003
Posts: 82
Where is it that you are getting stuck.

Also I dont seem to understand your requirements. Do you want to check if the string is even or odd or is it just the last word in the sentence that you want to check for evenness or oddness.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Hi Rose, I am going to move this thread to the Java In General(beginner) forum. This forum is for Servlets questions.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
I'm checking to count the entire string, including spaces to see if its even or odd. I have that part working.
If a user enters a question mark at the end of his/her sentence, it is supposed to output the word...yes...if the string is an even number, and it outputs the word...no...if it is an odd number. That part works as well, but it is also outputting the word..wow...and 'you always say that.. as well. It should stop after outputting the word...yes. Does that make since? It's hard to explain. Its only supposed to say yes or no if a user enters a question mark after his/her sentence. If they enter an exclamation mark it is supposed to say....Wow..and anything else they enter should say....'you always say that...follow by their string they entered. Does any of that make since? Im raking my brain trying to do it. Ha.

I'd appreciate any help.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks Mark, I'm new to this forum, never used one before in my life, so I am learning as I go. Thanks for moving it for me. I'll try to post here from now on.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, looks like you're on the way. Look at the API docs for indexOf() which you used and endsWith(). See if you like endsWith() better. Then try a few sentences and see if you sometimes get two responses, like "yes" and "wow". See if you can add a test for ? and organize your choices to get only one response.

I know those aren't real answers, but you got this far and it would be rude to spoil your opportunity to get the rest, too! Have fun, check back in with whatever you come up with.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Thanks Stan!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Please look at this code and see if any of you know what is wrong with it.
If I enter a sentence with a question mark and the number is even, it IS outputting the word yes, like it is supposed to, but it is also outputting the word Wow and 'you always say, wow.' I know that is hard to follow, heck I had to highlight the darn instructions in different colors just to follow it. Ha. It is also outputting the word no, if the sentence is an odd number of characters, like it should, but it also outputs the rest of the stuff too.
I just want it to output the word wow, if I end a sentence in an exclamation mark, and if I end a sentence with a period, I want it to output the last sentence its supposed to, without putting the other things with it. Does that make sense? It's driving me insane!!!
Also, it sure looks like I have a lot of these {} doesn't it?
I'd appreciate any help!


**************************************************************
//* This program will ask a user to input a sentence, then *
//* if the sentence ends with an even number of characters *
//* and a question mark, it will output the word 'Yes'. *
//* If the sentence ends with a question mark and it is an *
//* odd number of characters, it will output the word 'No'. *
//* If the sentence ends with an exclamation mark, the output *
//* will say 'Wow!'. In all other cases, it will output *
//*'You always say that.' *
//**************************************************************
//* Author: Rose Evans *
//* Date last modified: October 13th 2004. *
//**************************************************************
public class OneLineSentence
{
public static void main(String[] args)
{

String sentence;


System.out.println("Please enter any sentence of your choice.");
sentence = SavitchIn.readLine();
sentence.length();

System.out.println("The length of the sentence is: "+ sentence.length());
{
if (sentence.length() % 2 == 0)


{
System.out.println("Yes");

}

else
if (sentence.length() % 2 == 1)
{

System.out.println("No");
//System.exit(0);

}


{
{


if(sentence.endsWith("!"));


{

System.out.println("Wow");


}



System.out.println("You always say, " + sentence);
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
notice anything funny at the end of this line?
if (sentence.endsWith("!"));
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rose Evans:
if the sentence ends with an even number of characters
and a question mark, it will output the word 'Yes'.
Maybe it's just me, but I read that as "if the sentence ends with a question mark and its last word has an even number of characters."
If the sentence ends with a question mark and it is an
odd number of characters, it will output the word 'No'.
However, the second requirement makes it sound like the full sentence length should be measured.

I like to begin by creating test cases (a.k.a. sample data) to see how it would work. Assuming it's the last word that should be measured, you'd have the following.As for making multiple logical choices and displaying only one response, if and else-if will be helpful here.Only one block -- A, B or C -- will execute based on conditions a and b. An example is "I will eat if I am hungry, or go shopping if I need food, or watch a DVD otherwise. This is equivalent toGet used to both forms as they are both appropriate depending on the logic flow.

Look for commonalities in your logic statements and simplify them:can be rewrittenFinally, the [ code ] and [ /code ] UBB tags (no spaces) will preserve your indentation and make it easier to read your code. You can insert them with the CODE button below the edit box or type them.
[ October 17, 2004: Message edited by: David Harkness ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Programming project...