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ループの後にある必要があるため、あなたの場合のように複数回ではなく1回だけ出力されます。

于 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 を 2 次元配列にするでしょう。

幸運を!

于 2013-09-04T04:47:45.327 に答える