0

So I have this program I need to write. I'm, supposed to get an input string from a user and then print out how many capital letters and how many lowercased letters are in the string. I've looked everywhere in the book that I have and I just can't seem to find anything about how to print out the uppercase and lowercase letters. I've been doing a lot of googling as well and I couldn't find anything useful.

Anyway here's my code:

import java.util.Scanner; //calls out the method to get input from user

public class Verk1 {
    public static void main(String args[])
    {
        Scanner innslattur = new Scanner(System.in); //input gotten from user

        System.out.println("Sláðu inn textabrot í há- og lágstöfum.");
        System.out.println("Forritið mun þá segja þér hve margir stafir eru af hverri gerð.");
        System.out.println("Textabrot: ");

//The printouts before tell the user to enter in a string, the program will then print out //how many upper- and lowercase letters there are.

        String strengur = innslattur.nextLine();

        String hastafir = "";

        for (int i=0; i<hastafir.length();i++);
        {
            System.out.println("Í textabrotinu eru " + hastafir + " hástafir");
        }


    }
}

I know the code is faulty/doesn't work, but do any of you know how I get the number of uppercase- lowercase letters to print them out?

Thanks in advance! Cheers

Birgir
  • 49
  • 1
  • 4
  • 12
  • Check Java Program to test if a character is uppercase: http://stackoverflow.com/questions/8962025/java-program-to-test-if-a-character-is-uppercase-lowercase-number-vowel – VonSchnauzer Mar 11 '14 at 18:57
  • 1
    Iterate through the characters using [`String.charAt(i)`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt%28int%29) and then use [`Character.isUpperCase(char c)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isUpperCase%28char%29) to keep a count. Count lower case the same way in the same loop, make sure to account for spaces and non letter chars. – turbo Mar 11 '14 at 18:59

5 Answers5

0

You can do their fairly easily if you convert the string to a char[] first. You can then use the isUpperCase(char c) for each character in the string. http://www.tutorialspoint.com/java/character_isuppercase.htm

9er
  • 1,604
  • 3
  • 20
  • 37
0
public void printCapsAndLowercaseCounts(String s) {
    int uppercase = 0;
    int lowercase = 0;
    if (s != null) {
        String s1 = s.toUpperCase();
        String s2 = s.toLowerCase();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == s1.charAt(i) ^ s.charAt(i) == s2.charAt(i)) {
                if (s.charAt(i) == s1.charAt(i)) uppercase++;
                else lowercase++;
            }
        }
    }
    System.out.println(uppercase + " " + lowercase);
}
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • 1
    Why would you create two copies of the string, one upper and one lower case, instead of just checking if each character `isUpperCase` or `isLowerCase`? Also, the use of XOR here is confusing, and the `s1` and `s2` variables are not scoped correctly and the names are unhelpful. This code won't even compile. – David Conrad Mar 11 '14 at 19:05
  • I would also add (unnecessary) parentheses around the equality tests. I had to look up the relative precedence of == vs. ^ to be sure that was correct. – David Conrad Mar 11 '14 at 19:19
0

For some strange reason your for loop is referring to an empty string you've just declared, rather than the string you just read in from the user. However, if you change that, inside your loop you can get at the individual characters in the string with strengur.charAt(i) and you can test whether a letter is capital with Character.isUpperCase(ch) and you can check for a lower case letter with Character.isLowerCase(ch).

David Conrad
  • 15,432
  • 2
  • 42
  • 54
0

I haven't tested it but I would look to do something like this.

String text = "This IS My TEXT StrinG";
int upperCaseCounter = 0;
int lowerCaseCounter = 0;
for (int i=0; i<text.length(); i++)
   {
   if (Character.isUpperCase(text.charAt(i)))
      {
       upperCaseCounter++;
      }
   else if(Character.isLowerCase(text.charAt(i)))
     {
       lowerCaseCounter++;
     }
 }

System.out.println("Total Uppercase Characters: " + upperCaseCounter);
System.out.println("Total Lowercase Characters: " + lowerCaseCounter);
tino
  • 16
  • 1
  • Cheers! This worked, but it has one flaw. When it prints out the number of lowercase letters, it prints out all letters, including spaces. Know why? – Birgir Mar 11 '14 at 19:18
  • Glad it works for you! What exactly does it print out? text.length()? – tino Mar 11 '14 at 20:26
  • Yeah, it prints out the length of the string instead of just the number of lowerCase letters – Birgir Mar 12 '14 at 19:29
  • Sorry, it was my mistake, the code you wrote works just fine. I accidentally put a semicolon behind the else if statement. – Birgir Mar 13 '14 at 20:11
  • Ah...happy to hear it! I truly couldn't figure it out :) – tino Mar 13 '14 at 20:49
0

Seems like this would do the trick, assuming you're not doing it an excessive amount. Just use a temporary string, and get the difference between the two:

int capLetterCount = originalString.length() - originalString.replaceAll("[A-Z]", "").length();

aliteralmind
  • 19,847
  • 17
  • 77
  • 108