Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Java program

 
Sarah Creech
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code and explain where you are stuck.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sarah Creech
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using String.charAt() instead is probably more efficient, and more closely reflects what you are trying to do:


Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply
  • Bookmark Topic Watch Topic
  • New Topic