0

I have a sequence of words in a text file for my project. I'm trying to distinguish the Capital letters from the file, and only print out the biggest number that it can find. for example Input: Roll Tide Roll and my Output: 2 R

I think there is code for finding the max count or something, but I'm lost as of now.

Here is my code I have so far:

import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class letters {

   public static void main(String[] args) throws FileNotFoundException {
      FileInputStream fis = new FileInputStream("input.txt");
      Scanner scanner = new Scanner(fis);
      String str = scanner.nextLine();
      System.out.println(str);

      int upperCaseCounter = 0;

      int upperCase[] = new int[26];

      while (scanner.hasNextLine()) {
         String s = scanner.nextLine();
         for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isAlphabetic(ch)) {
               if (Character.isUpperCase(ch)) {
                  upperCase[ch - 'A']++;
                  System.out.println(ch + " : " + upperCase[ch - 'A']);
               }
            }
         }
      }
   }
}

my output is giving me something on the lines of:

R : 10
O : 6
L : 7
L : 8
R : 11
T : 5
R : 12

I just need to ONLY print the R: 12

How do you go by doing this. Any help would be greatly appreciated. Thanks! I'm new to the indentations on this site and was trying to be quick...

4

4 回答 4

4

您可以使用Arrays#sort方法来查找数组中的最大值或最小值。

Arrays.sort(upperCase);
int maxIndex = upperCase.length-1;
System.out.println("Max element is:"+(char)upperCase[maxIndex])+":"+upperCase[maxIndex]);  

sort()方法按升序对数组进行排序。那么数组的第一个元素是min数字,数组的最后一个元素是max

注意:上面的代码应该在 while 循环之后,以便它只打印一次而不是像您的情况那样打印多次。

于 2013-09-04T04:37:34.827 回答
1

或者,您可以在 for 循环中计算最大值。请运行我的代码。

   public static void main(String[] args) throws FileNotFoundException {
      FileInputStream fis = new FileInputStream("input.txt");
      Scanner scanner = new Scanner(fis);
      String str = scanner.nextLine();
      System.out.println(str);

      int upperCaseCounter = 0;

      int upperCase[] = new int[26];
      int max=0;
      char let='A';
      while (scanner.hasNextLine()) {
         String s = scanner.nextLine();
         for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
                if (Character.isAlphabetic(ch)) {
                    if (Character.isUpperCase(ch)) {
                        upperCase[ch - 'A']++;
//                        System.out.println(ch + " : " + upperCase[ch - 'A']);
                        if(max<upperCase[ch - 'A']){
                            max=upperCase[ch - 'A'];
                            let=ch;
                        }
                    }
                }

            }

        }
        System.out.println(let+"   "+max);
    }
}
于 2013-09-04T05:04:15.943 回答
0

在字符递增后,您可以在 while 循环中使用变量,例如 MaxVal。然后使用 if 语句将新分配的递增值 (upperCase[ch-'A']) 与变量 MaxVal 进行比较。如果大于 MaxVal,则将 MaxVal 赋值为 upperCase[ch-'A']

您可能会使 MaxVal 成为一个二维数组来保存字符及其当前计数

祝你好运!

于 2013-09-04T04:47:45.327 回答
0

您需要保留两个局部变量 int temp 和 char ch1 来跟踪您的最大长度和相应的校正器。我在这里给出修改后的代码。包 com.mindtree.programs;

导入 java.util.Arrays;导入 java.util.Scanner;

导入 java.io.FileInputStream;导入 java.io.FileNotFoundException;

公共类 ReadSccnner {

public static void main(String[] args) throws FileNotFoundException {
    FileInputStream fis = new FileInputStream("input.txt");
    Scanner scanner = new Scanner(fis);
    int upperCase[] = new int[26];
    int temp = 0;
    char ch1 = 0;
    while (scanner.hasNextLine()) {
        String s = scanner.nextLine();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);

            if (Character.isLetter(ch)) {
                if (Character.isUpperCase(ch)) {
                    upperCase[ch - 'A']++;
                    if (temp < upperCase[ch - 'A']) {
                        ch1 = ch;
                       temp = upperCase[ch - 'A'];
                    }

                }
            }
        }
    }
    Arrays.sort(upperCase);
    System.out.println("Max element is:" + ch1 + " : "
            + upperCase[upperCase.length - 1]);
}

}

于 2013-09-04T05:52:12.720 回答