I am having trouble making this code behave correctly.
I am trying to split up a compound sentence assuming:
1. AND is the only word that can create compound sentences.
2. Any time that AND is used it is a compound sentence.
3. A compound sentence can only have 1 AND in it.
When I run the program and type in a sentence, the result only returns the first word in the sentence. Please help. My java skills are a little rusty.
Code:import java.util.StringTokenizer; import java.util.Scanner; public class BreakComp { public static void main(String[] args) { Scanner user_input = new Scanner(System.in); String sentence; System.out.println("Please enter a compound sentence: "); sentence = user_input.next(); if(sentence.contains("and")){ sentence = sentence.replace(" and ", "|"); System.out.println(tokenizeSentence(sentence)[0]); System.out.println(tokenizeSentence(sentence)[1]); } else{ System.out.println(sentence.toUpperCase()); } } public static String[] tokenizeSentence(String s){ String[] sentenceString = new String[2]; StringTokenizer tok = new StringTokenizer(s, "|"); sentenceString[0] = tok.nextToken(); sentenceString[1] = tok.nextToken(); return sentenceString; } }




Reply With Quote
