mercredi 26 octobre 2016

Java: Counting Whitespace and Tabs

I am having trouble figuring out exactly how to get Java to spit out the amount of white spaces and tabs that are in a user inputted string. I used isWhiteSpace and all it counts is the amount of characters in the string.

I'd appreciate any help,

Thank you.

My Code:

import java.util.Scanner;

public class CharsInLine 
{
    public static void main(String[] args) 
    {
        Scanner console = new Scanner(System.in);

        int numberUpperCase = 0;
        int numberLowerCase = 0;
        int numberWhiteSpace = 0;

        System.out.print("Enter a line of text : ");
        String inLine = console.nextLine();
        int i = 0;

        while ( i < inLine.length() ) {

            char currentCharacter = inLine.charAt(i);

            if ( Character.isUpperCase(currentCharacter) ) {
                numberUpperCase++; }
            if ( Character.isLowerCase(currentCharacter) ) {
                numberLowerCase++; }
            if( Character.isWhitespace(currentCharacter) ); {
              numberWhiteSpace++; }

            i++;

        }

        System.out.println("There are " + numberUpperCase + " upper case characters, " + numberLowerCase + " lower case characters, and " + numberWhiteSpace + " white space characters.");

        console.close(); }
}

Aucun commentaire:

Enregistrer un commentaire