0

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.

3
  • So you want a running tally?? ie the first line would display "2", the second would display "4" ...etc, or would you just like to display it for the individual row? Oct 5, 2012 at 6:26
  • 1
    "So you want a running tally??" - I read that as "So you want to run Italy??" :)
    – Rich
    Oct 5, 2012 at 6:44
  • No, What I want to do is on the 3rd column, display the number of characters without counting the spaces.
    – user1410081
    Oct 5, 2012 at 6:45

4 Answers 4

2

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...

1
  • Do you mean "the number of characters in the name without counting the spaces"? - yeah. I just edited my post. thanks for clarifying.
    – user1410081
    Oct 5, 2012 at 6:28
2

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.

2
  • Yeah I have coded the table, but what I'm missing is the counting part of characters.
    – user1410081
    Oct 5, 2012 at 6:31
  • Thanks, I've just figured it out with all your help!~
    – user1410081
    Oct 5, 2012 at 7:02
0

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

4
  • I mean in my above issue, a 2D array? like this: String[][] students = { { "Carol McKane", "James Eriol", "Elainee Black" }, { "920", "154", "462" } };
    – user1410081
    Oct 5, 2012 at 6:43
  • again updated it. anyways your array design is pretty poor, I'd recommend to change it to String[][] students = { { "Carol McKane", "920"} , {"James Eriol", "154" }, {"Elainee Black", "462" }};
    – Naytzyrhc
    Oct 5, 2012 at 6:57
  • It would be easier to iterate over the data if each data set "name id" is kept together :)
    – Naytzyrhc
    Oct 5, 2012 at 7:13
  • so how will I display it in two columns?
    – user1410081
    Oct 6, 2012 at 9:05
0

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.

Your Answer

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