Forums Register Login

Help with Java program

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Please post your code and explain where you are stuck.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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("?"))

System.out.println("Yes");
else
System.out.println("No");

if (sentence.equals("!"))
System.out.println("Wow");
else
System.out.println("You always say " + sentence);

}
}
+Pie Number of slices to send: Send
 

Originally posted by Sarah Creech:


// 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("?"))

System.out.println("Yes");
else
System.out.println("No");

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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
sentence = SavitchIn.readLine();

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
+Pie Number of slices to send: Send
You are not currently performing the correct tests on the user input. The following code will help you on your way.

Keep posting if you get stuck again
[ October 08, 2004: Message edited by: Nigel Browne ]
+Pie Number of slices to send: Send
Using String.charAt() instead is probably more efficient, and more closely reflects what you are trying to do:


Layne
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 4037 times.
Similar Threads
java project
Programming project...
Scanner methods
Need help please!!
Taking input with Scanner : need to exit on pressing enter key
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:46:42.