Home > Back-end >  String index out of range..i cant find the problem so the program run
String index out of range..i cant find the problem so the program run

Time:10-11

Give a string: apple and apple
Longest substring: again
Longest substring size: 5

I can't find where the problem is if is my code wrong. the program wants us to find a string that is repeated two times

package as;
import java.util.Scanner;

public class askisii {
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        String stA,stB = null;
        int i,count=0,j,p1,p2;
        System.out.println("Give a string:");
        stA=scan.nextLine();
        
        for(i=0;i<stA.length()-1;i  ) {
            for( j=i 1;j<stA.length()-1;j  ) {

                if((stA.charAt(i)==stA.charAt(j)) && (stA.charAt(i 1)==stA.charAt(j 1))) {
                    stB =stA.charAt(i);
                    p1=i;
                    p2=j;
                    
                    while(stA.charAt(p1 1)==stA.charAt(p2 1)) {
                        stB =stA.charAt(p1 1);
                        p1  ;
                        p2  ;
                        count  ;
                    }
                    
                    i=stA.length();
                    j=stA.length();
                }
            }
        }
        
        System.out.println("Longest substring:"   stB);
        System.out.println("Longest substring size:"   count);
        scan.close();
    }

}

CodePudding user response:

In your while loop, you did not check whether or not the index had gone too far. Your while loop traveled to the end of the string and still tried to grab more characters when there weren't any to grab.

Switch your while loop to look like this. I think it will solve this problem.

while(p1 1 < stA.length() && p2 1 < stA.length() && stA.charAt(p1 1) == stA.charAt(p2 1)) {

CodePudding user response:

A more simple way to find the words with maximum no. of occurrence and with maximum length/size

// yourString is the string that you enter. 
// Split this string by whitespaces, gives you an array of all words
String[] words = yourString.split("\\s ");

// Put in the map word against its no. occurrence count in the string
Map<String, Integer> wordsCount = new HashMap<>();
for(String word : words) {
    wordsCount.put(word, wordsCount.get(word) == null ? 1 : wordsCount.get(word)   1);
}

// Get the max occurrence count value amongst all words
int maxOccurrenceCount = Collections.max(wordsCount.values());

int maxLength = 0;

List<Map.Entry<String, Integer>> maxOccurrenceWordEntries = new ArrayList<>();

// Separate out the word with max occurrence and find the max length amongst the words
// with max no. of occurrences
for(Map.Entry<String, Integer> wordEntry : wordsCount.entrySet()) {
    Integer occurrenceCount = wordEntry.getValue();

    if(maxOccurrenceCount == occurrenceCount && wordEntry.getKey().length() >= maxLength) {
        maxOccurrenceWordEntries.add(wordEntry);
        maxLength = wordEntry.getKey().length();
    }
}

// Finally print the words from the max no. occurrences, those whose length matches the maxLength
for(Map.Entry<String, Integer> wordEntry : maxOccurrenceWordEntries) {
    if(wordEntry.getKey().length() == maxLength) {
        System.out.printf("Word : %s | Count : %d | Size : %d \n",
                wordEntry.getKey(), wordEntry.getValue(), wordEntry.getKey().length());
    }
}
  • Related