質問

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

役に立ちましたか?

解決

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);

他のヒント

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

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

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);
}

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();

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top