package newpackage; import java.util.Arrays; import java.util.Scanner; public class PoolBalls { public static void main (String[] args) { @SuppressWarnings("resource") Scanner input = new Scanner(System.in); System.out.print("Please input a string that is at most 30 characters long \nand doesn't include uppercase letters: "); String inputstring = input.nextLine(); int[] numchar = new int[256]; int x = inputstring.length(); if (inputstring.length() > 30) { System.out.println("Sorry, your input is too long, please try again."); System.exit(0); } else if ((inputstring.toLowerCase()).equals(inputstring) == false) { System.out.println("Sorry, your input has uppercase letters, please try again."); System.exit(0); } for (int y = 0; y < x; y++) { numchar[inputstring.charAt(y)]++; } int charshow = -1; char mostchar = ' '; for (int y = 0; y < x; y++) { if (charshow < numchar[inputstring.charAt(y)]) { charshow = numchar[inputstring.charAt(y)]; mostchar = inputstring.charAt(y); } } String[] poolballs = inputstring.split("" + mostchar); int ballnum = poolballs.length; System.out.println("Your hit ball (splitter) is: " + mostchar); System.out.println("You have " + ballnum + " poolballs (splits)."); System.out.print("These are your poolballs (splits): "); System.out.println(Arrays.toString(poolballs)); int ballength = -1; String mostlength = null; for (int z = 0; z < ballnum; z++) { if (ballength < poolballs[z].length()) { ballength = poolballs[z].length(); mostlength = poolballs[z]; } else if (ballength == poolballs[z].length()) { int cmprsumone = 0; int cmprsumtwo = 0; for(int i = 0; i < mostlength.length(); i++){ cmprsumone = cmprsumone + (int) mostlength.charAt(i); } for(int i = 0; i < poolballs[z].length(); i++){ cmprsumtwo = cmprsumtwo + (int) poolballs[z].charAt(i); } if (cmprsumone < cmprsumtwo) { mostlength = poolballs[z]; } } } int ballengthmini = 31; String mostlengthmini = null; for (int z = 0; z < ballnum; z++) { if (ballengthmini > poolballs[z].length()) { ballengthmini = poolballs[z].length(); mostlengthmini = poolballs[z]; } else if (ballengthmini == poolballs[z].length()) { int cmprsumthree = 0; int cmprsumfour = 0; for(int i = 0; i < mostlengthmini.length(); i++){ cmprsumthree = cmprsumthree + (int) mostlengthmini.charAt(i); } for(int i = 0; i < poolballs[z].length(); i++){ cmprsumfour = cmprsumfour + (int) poolballs[z].charAt(i); } if (cmprsumthree > cmprsumfour) { mostlengthmini = poolballs[z]; } } } if((ballength == 0) || (ballengthmini == 0)) { System.out.println("Sorry, your longest and/or shortest ball had no length, please try again."); System.exit(0); } System.out.println("Here is the smallest ball (split length): " + mostlengthmini); System.out.println("Here is the largest (split length): " + mostlength); System.out.println("The difference between the highest and lowest value balls (split lengths) is: " + (ballength-ballengthmini)); float finalcomp = (float) ((float)(ballength-ballengthmini)/ballnum); System.out.println("Your final computation is: " + finalcomp); } }