Hi,
I’ve just written a simple code for splitting a sentence into its words but the didn’t work with ST3’s build & run system. Later on, I decided to test the code in NetBeans and it just worked fine!
What may the problem be? Does anyone have any idea?
Thanks in advance
Here is the code:
import java.util.Scanner;
public class problem1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a sentence");
String sentence = input.nextLine();
// Method 1 for splitting the sentence into its each word
char space = ' ';
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) != space) {
System.out.print(sentence.charAt(i));
}
else {
System.out.println();
}
}
// Method 2 for splitting the sentence into its each word
String word = "";
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) != space) {
word = word.concat(String.valueOf(sentence.charAt(i)));
}
else {
System.out.println(word);
word = "";
}
}
System.out.println(word);
}
}