0

I need to following program to output whatever string is input with each word on a new line and to count the total words. This so far outputs each letter on a new line. I used text.split to count the words and tried to add this into my for loop but it won't work, maybe because the for loop is Integer but I can't find how to add this in.

public class TextTest {

    public static void main(String[] args) {
        String text = Input.getString("Text: ");
        String[] separated = text.split(" ");//separates by spaces 

        do {
            if (text.isEmpty()) {
                System.out.println("empty text");
            } else {
                for (Integer position = 0; position < text.length(); position++) {

                    System.out.println(text.charAt(position));
                }
            }
            int WordCount = separated.length;
            System.out.println("number of words: " + WordCount);
        } while (Repeat.repeat());
2
  • "but it won't work" You need to be more detailed. Is there an error? Exception? Wrong output? What is it doing? Commented Jan 24, 2018 at 15:49
  • As @csmckelvey suggested, if you add your possible errors or output - your question might get upvoted as being beneficial and well formed Commented Jan 24, 2018 at 16:24

5 Answers 5

4
public static void main(String[] args) {
        String text = "THIS IS A SAMPLE TEXT";
        String[] separated = text.split(" ");//separates by spaces 

            if (text.isEmpty()) {
                System.out.println("empty text");
            } else {
                for (Integer position = 0; position < separated.length; position++) {

                    System.out.println(separated[position]);
                }
            }
            int WordCount = separated.length;
            System.out.println("number of words: " + WordCount);
    }

Your code does not work because you are using the text to output the words instead of the new array that you created and separated the words in it. This would work because each word lies in an index in the newly created array. Hope i helped!

Sign up to request clarification or add additional context in comments.

Comments

3
public static void main(String[] args) {
    String text = "hello I am here, how are  you?";
    String[] separated = text.split(" ");//separates by spaces

    for (String word : separated) {
        if (!word.trim().isEmpty()) {
            System.out.println(word);
        }
    }
}

Comments

3

With Lambdas this would be as easy as the following:

public static void main(String[] args) {
    String text = "THIS IS A SAMPLE TEXT";
    String[] splitted = text.split(" ");

    System.out.println("Number of words: " + Stream.of(splitted).count());
    Stream.of(splitted).forEach(System.out::println);
}

2 Comments

Downvoted. First, this doesn't compile. text.split(" ") should have a semicolon after it. Second, String::split requires a parameter and doesn't compile like this. Third, your solution doesn't do the requested operation.
@ifly6 You are totally right, thanks for the hint. Had another solution before and changed it on the fly hence creating this errors. I've update my code to create the correct version.
2

You could change the for like below

String[] splits = text.split("\\s+"); // split by one or more spaces.
for (String split: splits){
    System.out.println(split);
}
System.out.println("Word Count: " + splits.length);

The count of words will be split count. Also you don't need to consider empty splits as the RegExp will handle cases where the words are separated by more than one spaces.

1 Comment

This is probably the simplest and straight forward solution to OP's question.
1

Using Java 8 streams, this would do it. It

String text = "THIS IS A SAMPLE TEXT";
long count = Stream.of(text.split("\\s+")) // split based on whitespace
        .peek(System.out::println) // print each one
        .count(); // count them
System.out.println("Word count: " + count); // print the count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.