user1410081
user1410081

Reputation:

Java characters count in an array

Another problem I try to solve (NOTE this is not a homework but what popped into my head), I'm trying to improve my problem-solving skills in Java. I want to display this:

Students               ID    #
Carol McKane    920    11
James Eriol        154    10
Elainee Black       462    12

What I want to do is on the 3rd column, display the number of characters without counting the spaces. Give me some tips to do this. Or point me to Java's robust APIs, cause I'm not yet that familiar with Java's string APIs. Thanks.

Upvotes: 0

Views: 4300

Answers (4)

DLade
DLade

Reputation: 63

To learn more about it you should watch the API documentation for String and Character

Here some examples how to do:

// variation 1
int count1 = 0;
for (char character : text.toCharArray()) {
    if (Character.isLetter(character)) {
        count1++;
    }
}

This uses a special short from of "for" instruction. Here's the long form for better understanding:

// variation 2
int count2 = 0;
for (int i = 0; i < text.length(); i++) {
    char character = text.charAt(i);
    if (Character.isLetter(character)) {
        count2++;
    }
}

BTW, removing whitespaces via replace method is not a good coding style to me and not quite helpful for understanding how string class works.

Upvotes: 0

Naytzyrhc
Naytzyrhc

Reputation: 2317

Probably the shortest and easiest way:

    String[][] students = { { "Carol McKane", "James Eriol", "Elainee Black" }, { "920", "154", "462" } };


    for (int i = 0 ; i < students[0].length; i++) {
        System.out.println(students[0][i] + "\t" + students[1][i] + "\t" + students[0][i].replace( " ", "" ).length() );
    }

replace(), replaces each substring (" ") of your string and removes it from the result returned, from this temporal string, without spaces, you can get the length by calling length() on it... The String name will remain unchanged.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

cheers

Upvotes: 0

Cory Kendall
Cory Kendall

Reputation: 7324

Think of solving a problem and presenting the answer as two very different steps. I won't help you with the presentation in a table, but to count the number of characters in a String (without spaces) you can use this:

String name = "Carol McKane";
int numberOfCharacters = name.replaceAll("\\s", "").length();

The regular expression \\s matches all whitespace characters in the name string, and replaces them with "", or nothing.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503539

It sounds like you just want something like:

public static int countNonSpaces(String text) {
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) != ' ') {
            count++;
        }
    }
    return count;
}

You may want to modify this to use Character.isWhitespace instead of only checking for ' '. Also note that this will count pairs outside the Basic Multilingual Plane as two characters. Whether that will be a problem for you or not depends on your use case...

Upvotes: 2

Related Questions