Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Programming project...

 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rose, I am going to move this thread to the Java In General(beginner) forum. This forum is for Servlets questions.

Thanks

Mark
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan!
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notice anything funny at the end of this line?
if (sentence.endsWith("!"));
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic