-1

I am trying to write a program Count.java that counts the characters, newlines, and words in a string input by the user with a return value that is an array of the three counts.

I've written a program that is supposed to store a string entered by the user from the main method as the variable text in the method public static int[] analyze(String text) and, then, analyze it with the three integer count variables wordCount, charCount, and lineCount.

public class Count { 

    public static void main(String[] args) {
        System.out.print("Please enter a string: ");
        System.out.println(arr);
        System.out.println("Total number of words: " + arr[0]);
        System.out.println("Total number of characters: " + arr[1]);
        System.out.println("Total number of newlines: " + arr[2]);

    }

    public static int[] analyze(String text) {
        Scanner in = new Scanner(System.in);
        String text = in.nextLine();
        int wordCount = 0;
        int charCount = 0;
        int lineCount = 1;
        int[] arr;
        arr = new arr[] {wordCount, charCount, lineCount}; 

        for (int i = 0; i < text.length(); i++) {

            if (text.charAt(i) == ' ' && text.charAt(i + 1) != ' ') {
                wordCount++;
            }

            if (text.charAt(i) != ' ') {
                charCount++;
            }
        }

        if (text.charAt(i) == text.charAt(i)) {
            lineCount++;
        }

        return arr;
    }

}

I want it to output the number of characters, newlines, and words of the user's input string. However, when I try to run it, the compiler doesn't recognize my string variable text and array arr. Any suggestion for how I might fix this?

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Jess B.
  • 11
  • 2

2 Answers2

0

I think you should analyse input data on new string available, because not to store it in one bit String object. Another note is to use Data object instead of an array; this is more intuitive:

public class Count {

    public static void main(String... args) {
        try (Scanner scan = new Scanner(System.in)) {
            System.out.print("Please enter a string (empty line to stop): ");

            Data data = analyze(scan);

            System.out.println("Total number of words: " + data.totalWords);
            System.out.println("Total number of characters: " + data.totalCharacters);
            System.out.println("Total number of newlines: " + data.totalNewLines);
        }
    }

    public static Data analyze(Scanner scan) {
        Data data = new Data();

        while (true) {
            String line = scan.nextLine();

            if (line.isEmpty())
                break;
            else {
                data.totalNewLines++;

                for (int i = 0; i < line.length(); i++)
                    if (!Character.isWhitespace(line.charAt(i)))
                        data.totalCharacters++;

                data.totalWords += line.split("\\s+").length;
            }
        }

        if (data.totalNewLines > 0)
            data.totalNewLines--;

        return data;
    }

    private static final class Data {

        private int totalCharacters;
        private int totalNewLines;
        private int totalWords;
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
0

Jess, There is my solution. Also I would suggest you to learn more java core and remember to try to google questions before you ask. Good luck in your journey :)

public class Count {

    public static void main(String[] args) {
        System.out.print("Please enter a string: ");
        Scanner in = new Scanner(System.in);
        String text = in.nextLine();
        int arr[] = analyze(text);
        System.out.println(arr);
        System.out.println("Total number of words: " + arr[0]);
        System.out.println("Total number of characters: " + arr[1]);
        System.out.println("Total number of newlines: " + arr[2]);

    }

    public static int[] analyze(String text) {
        int[] arr = new int[3];

        // count lines
        String[] lines = text.split("\r\n|\r|\n"); // taken from https://stackoverflow.com/questions/454908/split-java-string-by-new-line
        arr[2] = lines.length;

        // count number of words
        arr[0] = countWords(text);

        // count number of characters
        arr[1] = counteCharacters(text);
        return arr;
    }

    static final int OUT = 0;
    static final int IN = 1;

    static int countWords(String str)  // taken from https://www.geeksforgeeks.org/count-words-in-a-given-string/
    {
        int state = OUT;
        int wc = 0;
        int i = 0;

        while (i < str.length()) {
            if (str.charAt(i) == ' ' || str.charAt(i) == '\n'
                    || str.charAt(i) == '\t')
                state = OUT;

            else if (state == OUT) {
                state = IN;
                ++wc;
            }
            ++i;
        }
        return wc;
    }

    static int count = 0; // taken from https://www.javatpoint.com/java-program-to-count-the-total-number-of-characters-in-a-string

    static int counteCharacters(String text) {
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) != ' ')
                count++;
        }
        return count;
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92