Advertisement
Kuncavia

HeaviestWordLoop

Oct 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Judge.Arrays;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class HeaviestWordLoop {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = scanner.nextInt();
  9.         String[] words = new String[n];
  10.         //String[] words1 = { "hello", "heavy", "word" };
  11.  
  12.         int biggestSum = 0;
  13.         String biggestWord = null;
  14.  
  15.         for (int i = 0; i < words.length; i++) {
  16.             words[i] = scanner.nextLine();
  17.         }
  18.  
  19.         for (String word : words) {
  20.             int sum = 0;
  21.  
  22.             for (char ch : word.toLowerCase().toCharArray()) {
  23.                 if (ch >= 'a' && ch <= 'z') {
  24.                     sum += 1 + ch - 'a';
  25.                 }
  26.             }
  27.  
  28.             if (sum > biggestSum) {
  29.                 biggestSum = sum;
  30.                 biggestWord = word;
  31.             }
  32.             System.out.printf("%s %d%n", word, sum);
  33.         }
  34.         System.out.printf("Biggest word: %s %d%n", biggestWord, biggestSum);
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement