charond Richardson
charond Richardson

Reputation: 45

How to print out just the max number from a list of an array?

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

Upvotes: 0

Views: 728

Answers (4)

navin kumar
navin kumar

Reputation: 191

you need to keep two local variable int temp and char ch1 to track your max length and corresponding correcter . here i am giving the modified code . package com.mindtree.programs;

import java.util.Arrays; import java.util.Scanner;

import java.io.FileInputStream; import java.io.FileNotFoundException;

public class ReadScacnner {

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

}

Upvotes: 0

Prabhaker A
Prabhaker A

Reputation: 8483

You can use of Arrays#sort method to find max or min number in the array.

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

sort() method sorts the array in ascending order.Then the first element of array is min number and last element of array is max.

Note : The above code should be after the while loop so that it prints only one time instead of multiple times as in your case.

Upvotes: 4

Masudul
Masudul

Reputation: 21981

Alternatively you can count maximum inside your for loop. Please run my code.

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

Upvotes: 1

user2685803
user2685803

Reputation: 281

You can use a variable eg MaxVal inside the while loop after a character has been incremented. Then use an if statement to compare the newly assigned incremented value (upperCase[ch-'A']) to the variable MaxVal. If it is bigger than MaxVal, assign MaxVal the value of upperCase[ch-'A']

You would probably make MaxVal a 2 dimensional array to hold the character and its current count

Goodluck!

Upvotes: 0

Related Questions