Reputation: 29
What my code is trying to do, is print each word of a string on a newline. However, I have to limit that to only the first five words. Additionally, I have to add "first word: word" and so forth, for each of those five words. I'm not quite sure how to go about obtaining the results I desire.
In my code, I have:
int space = sentence.indexOf(" ");
sentence = sentence.substring(0,space) + "\n" + sentence.substring(space+1);
System.out.println(sentence.replaceAll("\\s+", "\n"));
Any help or guidance would be appreciated, thank you!
Upvotes: 0
Views: 112
Reputation: 18812
public static void main(String[] args){
final String[] prefix = {"st", "nd", "ed","th", "th"};
final String SPACE =" ", NL = "\n";
String sentance ="A sentence with five word or more";
int counter = 0;
for(String s: sentance.split(SPACE)){
if(++counter > prefix.length) break;
System.out.println(counter + prefix[counter-1] + " WORD: " + s + NL );
}
}
Upvotes: 0
Reputation: 1147
You could separate your Token
string and compare with some other string, if it meets replaces
the string and then prints the first 5 words with the requested format
and to print other words outside the top 5 could use a StringBuilder
StringBuilder bu = new StringBuilder();
String str = "This is String As a Compare DAD ADAS DAS";
StringTokenizer st = new StringTokenizer(str);
int x =0;
while (st.hasMoreElements()) {
String val = (String) st.nextElement();
x+=1;
if(val.equals("is"))val="NewValue"; //change word to other value
if(x<=5)System.out.println(" WORD N° " + x + " " +val);
else
bu.append(val).append(" ");
}
System.out.println(bu.toString());
Ouput
WORD N° 1 This
WORD N° 2 NewValue
WORD N° 3 String
WORD N° 4 As
WORD N° 5 a
Compare ABC DFG AHG
Upvotes: 0
Reputation: 2326
Quite a simple approach with just iterating and using arrays to keep track of words...
import java.util.Arrays;
public class WordSplit {
public static void main(String[] args){
printStrings("This is a test string", 2);
}
public static void printStrings(String sentence, int skip){
String[] splitSentence = sentence.split(" ");
String[] afterSplit = Arrays.copyOfRange(splitSentence, skip, splitSentence.length);
int c = 0;
for(int i=0; i<skip; i++){
System.out.println(ordinal(i+1)+" word: "+splitSentence[i]);
}
System.out.print("The rest of the sentence: ");
for(String s: afterSplit){
System.out.print(s+" ");
}
System.out.println();
}
public static String ordinal(int i) {
String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
switch (i % 100) {
case 11:
case 12:
case 13:
return i + "th";
default:
return i + sufixes[i % 10];
}
}
}
Output:
1st word: This
2nd word: is
The rest of the sentence: a test string
Upvotes: 0
Reputation: 559
I would do something like this
char[] sentanceChars = sentance.toCharArray();
StringBuilder sb = new StringBuilder();
int wordIdx = 1;
for(int i=0; i<sentance.length(); i++) {
if(sentanceChars[i] == ' ') {
System.out.println("word "+ (wordIdx++) +"="+ sb.toString());
if(wordIdx == 6) {
break;
}
sb = new StringBuilder();
} else {
sb.append(sentanceChars[i]);
}
}
Upvotes: 1