The method longeststreak is intended to determine the longest substring of consecutive identical characters in the parameter str and print the result. For example, the call longeststreak("ccaaaaattt!") should print the result "a 5" because the longest substring of consecutive identical characters is "aaaaa". Complete the method below. Your implementation should conform to the example above.

Respuesta :

The program is an illustration of methods

What are methods?

A Method in java is a block of named statements, that can be executed by simply calling it.

The longestStreak method

The method longestStreak in Java is as follows where comments are used to explain each line is as follows:

public static void longestStreak(String str){

//This initializes the initial character to an empty character

char previousChar = ' ';

 //This initializes the current and the longest streak to an empty string

 String largestStreak = "", currentStreak = "";

 //This iterates through the string

 for (int i = 0; i < str.length(); i++){

  //This checks if the current character and the previous character are not the same

if (str.charAt(i) != previousChar){

 //If yes, the streak is reset

  currentStreak = "";

   }

 //The streak continues here

  currentStreak += str.charAt(i);

      //This checks the length of the longest streak

      if (currentStreak.length() > largestStreak.length()){

          largestStreak = currentStreak;

      }

      //This sets the previous character to the current character

      previousChar = str.charAt(i);

     }

     //This prints the longest streak and its length

     System.out.println(largestStreak.charAt(0)+" "+largestStreak.length());

 }

Read more about similar programs at:

https://brainly.com/question/19360184

ACCESS MORE