Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Help with Java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Java program " Watch "Help with Java program " New topic
Author

Help with Java program

Sarah Creech
Greenhorn

Joined: Oct 08, 2004
Posts: 6
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


Sarah
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Please post your code and explain where you are stuck.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


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
Sarah Creech
Greenhorn

Joined: Oct 08, 2004
Posts: 6
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);

}
}
Sarah Creech
Greenhorn

Joined: Oct 08, 2004
Posts: 6
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.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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 ]
Sarah Creech
Greenhorn

Joined: Oct 08, 2004
Posts: 6
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
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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 ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Using String.charAt() instead is probably more efficient, and more closely reflects what you are trying to do:


Layne


Java API Documentation
The Java Tutorial
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
 
Consider Paul's rocket mass heater.
 
subject: Help with Java program
 
Similar Threads
Programming project...
Taking input with Scanner : need to exit on pressing enter key
Need help please!!
Scanner methods
java project