Program that takes a one-line sentence as input and then outputs the following response: If the sentence ends with a '?' and the input contains an even anount of characters then output the word 'Yes'. If the sentences ends with a '?' and has an odd amount of characters then output the word'No'. If the sentence ends with an '!' then output the word 'Wow'. In all other cases output the string "you always say " followed by the input string enclosed in quotes. The output should all be on one line. Be sure your output must include quotations around the input string. The program should have a loop that allows the user to repeat this until the user wants to quit. The program does not have to check to see if the user entered legitamate sentence. Anyones help will be greatly appreciated I have been working on this for a few days and can not figure it out. Thanks Sarah
I like to restate a problem like that in pseudo code ...
Then look for Java features that will do the various tasks and gradually morph that into code. You can do this one in almost exactly that many lines of Java. As Nigel said, feel free to share what you have so far when you get stuck.
Originally posted by Nigel Browne: Please post your code and explain where you are stuck.
// Program to take in one line sentence as input then outputs the following If the sentence ends with a // question mark and the input contains an even number of characters then output the word YES. // If the sentence ends with a question mark and the input contains an odd number of characters // output the word NO.
public class Sentence
{
public static void main(String[] args)
{ String sentence;
System.out.println("Please input a sentence with correct punctuation at the end of the sentence.");
sentence = SavitchIn.readLine(); if (sentence.equals("?"))
// Program to take in one line sentence as input then outputs the following If the sentence ends with a // question mark and the input contains an even number of characters then output the word YES. // If the sentence ends with a question mark and the input contains an odd number of characters // output the word NO.
public class Sentence
{
public static void main(String[] args)
{ String sentence;
System.out.println("Please input a sentence with correct punctuation at the end of the sentence.");
sentence = SavitchIn.readLine(); if (sentence.equals("?"))
if (sentence.equals("!")) System.out.println("Wow"); else System.out.println("You always say " + sentence);
} }
I am stuck because when I run this it is not picking up everything you can copy and paste in textpad to see what i mean. Thanks I am just very confused! My instructor tried to help me yesterday but I think she confused me even more.
I am confused by this line: Are you trying to read the standard input of your system ? If so, you need to utilise some of the java.io classes. Here is an example: [ October 08, 2004: Message edited by: Nigel Browne ]
is the way to get input. That is the way I learned but the instructor says it is not real world stuff. It is just to get input so just look over it. This is just my first semester with Java and I do not know much about it. I am just stuck on how to do what this problem is asking. I understand what it is asking just do not know how to do it. We are working out of a book named Java an Introduction to Computer Science and Programming. By Walter Savitch ISBN 0-13-101378-5 I am not a professional at this so just bear with me. Just tell me how you would do this problem. Thanks
I always liked String.endsWith() - no substring or length checks. You don't have the even / odd length checks in there yet either. Look for Java operator // and % and see which might tell you even or odd.
Post by:autobot
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, that will fit in a stocking