guys i really need your help about my project anyone can help me i will appreciate it because its due on 3/3/11.
thank you and GOD bless!

1.Write a program that takes a one-line sentence as input and outputs the following response: 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, then output the word “No”. If the sentence ends with an exclamation mark ‘!’, output the word “Wow”. In all other cases, your program will output the string You always say followed by the input string enclosed in quotes. Your output should all be on one line.

2.Be sure to note that in the last case, your output must include quotation marks around the echoed input string. In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user has entered a legitimate sentence. Test your program with at least 4 strings of different lengths and ending punctuation.

Dani AI

Generated

Three simple steps make the specification easy to implement and fix the common mistakes already visible in the thread: read the whole input line as a String, normalize it (trim trailing/leading whitespace), then inspect the last character and the string length. was correct to suggest switching from an integer input to a String; 's pointer to the String API is relevant here. The "compiles but no output" symptom in Post #10 came from using nextInt() and a numeric switch — that approach never captures a full sentence.

The decision logic is straightforward: if the trimmed string is empty, treat it as the “other” case (echo the quoted empty string). If the last character is ?, print Yes when the trimmed length is even and No when it is odd. If the last character is !, print Wow. In every other case print You always say "the input" (with quotes). Counting normally includes spaces and punctuation; to ignore spaces for parity, compute parity from line.replace(" ", "") instead.

import java.util.Scanner;

public class SentenceChecker {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine().trim();
        if (line.length() == 0) {
            System.out.println("You always say \"\"");
            return;
        }
        char last = line.charAt(line.length() - 1);
        if (last == '?') {
            System.out.println((line.length() % 2 == 0) ? "Yes" : "No");
        } else if (last == '!') {
            System.out.println("Wow");
        } else {
            System.out.println("You always say \"" + line + "\"");
        }
    }
}

Common pitfalls: using nextInt() (reads tokens, not full lines), forgetting to trim() trailing spaces, off-by-one errors with charAt(length - 1), and relying on a switch over a small set of integer cases instead of testing the last character. Test with at least four inputs that vary ending punctuation and length (for example: Hello?, Hi?, Wow!, Hello there.) to verify all branches.

Recommended Answers

All 12 Replies

You're not asking anyone to do your homework for you, are you? You know that's a no-no.

In the mean time you can you look at the java.lang.String API. There are methods you can use. I think there is one called "endsWith".

you expect us to help yet you havent even started

In the mean time you can you look at the java.lang.String API. There are methods you can use. I think there is one called "endsWith".

thanks for the info

guys i really need your help about my project anyone can help me i will appreciate it because its due on 3/3/11.
thank you and GOD bless!

1.Write a program that takes a one-line sentence as input and outputs the following response: 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, then output the word “No”. If the sentence ends with an exclamation mark ‘!’, output the word “Wow”. In all other cases, your program will output the string You always say followed by the input string enclosed in quotes. Your output should all be on one line.

2.Be sure to note that in the last case, your output must include quotation marks around the echoed input string. In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user has entered a legitimate sentence. Test your program with at least 4 strings of different lengths and ending punctuation.

i want to say sorry about this yeah your right i need to start first before i ask for help so thank you

Can anyone explain me to do this please i'm not asking for the code i just want to understand this problem,,, thanks

Can anyone explain me to do this please i'm not asking for the code i just want to understand this problem,,, thanks

Why don't you tell as in maybe some bullet points what you think you are asked to do? To understand project needs/requirement is one of the many things on long list of abilities of a programmer.

i was doing the program it compiles but i cant get the output

import java.util.*;

public class Sentence
{
	 static Scanner keyboard = new Scanner(System.in);
	 
	 public static void main(String[] args)
	 {
	    
	    int she;
	 	System.out.print("Enter you words:");	 	 	
	 	she = keyboard.nextInt();
	 	System.out.println();
	    
	 	
	 	
	 	switch (she)
	 	{
	 		case 2: case 4:case 6: System.out.println ("Even number of characters," 
	 		                            + "last character is '?'");
	 	    System.out.println("Input: " + she);
	 		    break;
	 		case 1: System.out.println("Odd number or characters,"
	 		                           + "last character is '?'");
	 	    System.out.println("Input: " + she); 
	 	    	break;
	 		case 5: System.out.println("Last character is '!'");
	 	    System.out.println("Input: " + she);	
	 	     	break;
	 		case 0: System.out.println("All other cases");
	 		 	break;
	 		default: System.out.println("You alyaws say "+ she);
	 	
	 	} 	
	 }

}

First thing first. Ask user for input, read input and store it in the string. After that you will use it to pass as parameter for different operations you will call on it.
So replace line 12 with string equivalent of your integer currently used.

i still cant get the string....

import java.util.*;

public class Sentence
{
	 static Scanner keyboard = new Scanner(System.in);
	 
	 public static void main(String[] args)
	 {
	    
	  String she;
	 	System.out.print("Enter you words:");	 	 	
	 	she = keyboard.nextLine();
	 	System.out.println(she);

		/** Make calls to bellow stated methods and act on the results. Do not forget to use trim method to remove any tailing empty spaces
		*/
	 }

	 public static boolean endsWithQuestionMark(String sentemce){
			//return true or false if ends with "?"
	 }

	 public static boolean endsWithExplanationMark(String sentence){
			//return true or false if ends with "!"
	 }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.