Question: hi i have this java program that returns the length of the largest run in the string. A run is a series of zero or

hi i have this java program that returns the length of the largest run in the string. A "run" is a series of zero or more adjacent characters that are the same. So the max run of "xxyyyz" is 3, and the max run of "xyz" is 1. I wrote the code but it doesnot run i dont know how to fix it

code:

import java.util.Scanner;

/** * This is the calculating heating time program. */ final class Run { /** * Prevent instantiation. * Throw an exception IllegalStateException. * if this ever is called * * @throws IllegalStateException * * */ private Run() { throw new IllegalStateException("Cannot be instantiated"); }

/** * The starting main() function. * * @param args No args will be used */ public static void main(String[] final args) { /** * This is the maxRun. * * @param str is good * @return largestRun */ static int maxRun(final String str) { int largestRun = 1; int temp = 1;

for (int word = 1; word < str.length(); word++) { if (str.charAt(word) == str.charAt(word - 1)) { ++temp; } else { largestRun = Math.max(largestRun, temp); temp = 1; } }

largestRun = Math.max(largestRun, temp);

return largestRun; }

String inputString = null; System.out.print("Enter string : "); final Scanner scanner = new Scanner(System.in); inputString = scanner.nextLine();

try { if (inputString.matches(".*\\d.*")) { throw new Exception("Incorrect input"); } else { System.out.println(maxRun(inputString)); } } catch (Exception ex) { System.out.println(ex.toString()); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!