Lev1athan909
Lev1athan909

Reputation: 11

Counting number of words and characters from several lines of user input

Description: "Write a program to read several lines of text (each line ending with a '.') from Scanner. It should count the number of words and the number of non-space characters. The program should stop accepting input when it reads the word (DONE) in a separate line of input."

I'm having trouble with this program. Whenever I input the sample text and type in DONE, the number of words and characters match that of only the word 'DONE'. If someone could provide a working fix, that would be very helpful towards my understanding of the problems at hand.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter text (type DONE to quit): ");
    String text = "";
    while (!"DONE".equals(text)) {
        text = input.next();
    }
    String[] numOfWords = text.split(" ");
    String[] numOfChars = text.split("");
    System.out.print("Number of words: " + numOfWords.length);
    System.out.println("");
    System.out.print("Number of non-space characters: " + numOfChars.length);
    }
}

Upvotes: 1

Views: 122

Answers (3)

JustAnotherDeveloper
JustAnotherDeveloper

Reputation: 2256

Both of the other answers fail to fix the error in counting the number of characters. The following code correctly accounts for lack of spaces, gets rid of the input termination text, and correctly counts the non-space characters:

import java.util.*;
public class MyClass {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter text (type DONE to quit): ");
        String text = "";
        String output = "";
        while (!"DONE".equals(text)) {
            text = input.next();
            output += text.replace("DONE", "") + " ";
        }
        String[] numOfWords = output.split(" ");
        int numOfChars = output.replace(" ", "").length();
        System.out.print("Number of words: " + numOfWords.length);
        System.out.println("");
        System.out.print("Number of non-space characters: " + numOfChars);
    }
}

Input: text with four words DONE

Output

Please enter text (type DONE to quit):

Number of words: 4

Number of non-space characters: 17

Upvotes: 1

Yeshwin Verma
Yeshwin Verma

Reputation: 454

In your code what happens is that at last you give the input is DONE so it counts only done use the following code

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter text (type DONE to quit): ");
    String text = "";
    while (true) {
        if(!text.equals("DONE")){
                 break;
         }
        text += " " + input.nextLine();
         
    }
    String[] numOfWords = text.split(" ");
    String[] numOfChars = text.split("");
    System.out.print("Number of words: " + numOfWords.length);
    System.out.println("");
    System.out.print("Number of non-space characters: " + 
     numOfChars.length);
}

}

Upvotes: 1

ScottFree
ScottFree

Reputation: 632

As the answer above is incorrect (doesnt compile or work) you could try this:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter text (type DONE to quit): ");
    String text = "";
    String output = "";
    while (!"DONE".equals(text)) {
        text = input.next();
        output += text.replace("DONE", "") + " ";
    }
    String[] numOfWords = output.split(" ");
    String[] numOfChars = output.split("");
    System.out.print("Number of words: " + numOfWords.length);
    System.out.println("");
    System.out.print("Number of non-space characters: " + numOfChars.length);
}

Effectively creating another storage for the input string and once the DONE keyword is supplied exit the loop after removing the word "DONE".

Upvotes: 2

Related Questions