使用字符串计算数组中的大写字符

时间:2018-02-23 05:27:26

标签: java arrays string

所以我试图用字符串计算数组中的大写字符数。我在这里的砖墙。如果有人能够对我的问题有所了解,那就太棒了。

我假设只用Character.isLowerCase(item)就可以完成同样的循环了吗?

完成此操作后,我还必须告诉用户数组中最长的字符串以及最长字符串的字符数,我真的不知道该怎么做。

教授真的向我们扔了一个曲线球......

所以我的代码到目前为止:

// Program3.java
// Brandin Yoder
// 2/23/18
// Store strings in an array and tell user number of upper-case and lower-case characters,
// and spaces
import java.util.Scanner;


public class Program3
{
  public static void main(String[] args)
  {
   // Set up keyboard.
   Scanner keyboard = new Scanner(System.in);

   // Input number of strings to store.
   System.out.print("Number of strings to input:  ");
   int nrStrings = keyboard.nextInt();

   // Clear keyboard buffer.
   keyboard.nextLine();

   // Set up array to hold strings.
   String[] strings = new String[nrStrings];

   // Input strings from keyboard.
   System.out.println("\nInput strings:");

   for(int ctr = 0; ctr < nrStrings; ctr++)
   {
     System.out.print("String #" + (ctr+1) + " :");
     strings[ctr] = keyboard.next();
   }

   // Print back strings input.
   System.out.println("\nStrings input:");

   for(int ctr = 0; ctr < nrStrings; ctr++)
   {
     System.out.println("String #" + (ctr+1) + ":  " + strings[ctr]);
   }

   // Set up variables for upper-case, lower-case and white space calculator.
   int UpperNr = 0;
   int LowerNr = 0;
   int Spaces = 0;

   // For loop that determines amount of Upper-Case numbers.
   for(int ctr = 0; ctr < nrStrings; ctr++)
   {
     char item = strings[ctr].charAt(ctr);

     if(Character.isUpperCase(item))
     UpperNr++;
   }

   System.out.println(UpperNr);
 }
}

3 个答案:

答案 0 :(得分:0)

您需要创建变量来保存最后要打印的数据。在这种情况下,您需要维护一个数组,该数组具有每个字符串的Uppercases数以及最长字符串的索引和长度。您必须使用嵌套的for循环来遍历您拥有的字符串数组以及字符串本身,以便检查您拥有多少个大写字符。我已经修改/评论了下面代码的最后一部分。

   //array that contains number of uppercase letters in each string
   int[] upperAmount = new int[nrStrings];
   //index of the longest string
   int maxLenIndex = 0; 
   //length of longest string
   int maxLength = 0; 

   //array that iterates through all the strings in the array strings[]
   for(int i = 0; i<strings.length;i++){
     //if the new string is the longest 
     if(strings[i].length() > maxLength){
       //set maxlength to the new length and record index of string
       maxLength = strings[i].length();
       maxLenIndex = i; 
     }
   // For loop that determines amount of Upper-Case numbers.
      for(int ctr = 0; ctr < strings[i].length(); ctr++)
      {
         char item = strings[i].charAt(ctr);
         if(Character.isUpperCase(item))
           UpperNr++;
      }
      //add number of uppercases to upperAmount array indexes will be the same
      upperAmount[i] = UpperNr; 
      //reset upper number
      UpperNr = 0; 
   }

   // Print back strings input.
   System.out.println("\nStrings input:");

   for(int ctr = 0; ctr < nrStrings; ctr++)
   {
     System.out.println("String #" + (ctr+1) + ":  " + strings[ctr]);
     System.out.println("Number of Uppercase Letters: " + upperAmount[ctr]);
   }

   System.out.println("MaxStringLength: " + maxLength);
   System.out.println("Max String: " + strings[maxLenIndex]);

  }

答案 1 :(得分:0)

我希望这能解决你的问题

//after you finish printing the strings
           String strMax="";
           int ctr=0;
           for(String str :strings ){
               strMax = str.length()>strMax.length()?str:strMax;
               if(!str.equals(str.toLowerCase())){
                   for(char c : str.toCharArray()){
                       if(Character.isUpperCase(c))
                           ctr++;
                   }
               }
           }
           System.out.println("Longeset String"+strMax );
           System.out.println("total Upper case chars" +ctr);

答案 2 :(得分:0)

输入字符串的数量是否必要?我认为你可以接受一个完整的字符串并将其转换为字符数组 char[] charArray = acceptedString.toCharArray; 然后抛出所有字符,charArray[n] > 64 && charArray[n] < 91将变量增加到计数UpperCases。希望你理解)问你是否有问题。

Scanner keyboard = new Scanner(System.in);
String inString = keyboard.nextLine();

char[] symbol = inString.toCharArray();

int count =0;

for(int i =0; i < symbol.length; i++){
  if(symbol[i] > 64 && symbol[i] < 91){ //cause every char has its own number in Unicode. 'A' = 65 and 'Z' = 90
    count++;
  }
}

System.out.print(count);