Daniel Doboș
Daniel Doboș

Reputation: 81

String index out of range and can't find anything similar

I have to find the char which appears the least in a string.

package String2;

public class Methods3 {
  static final int chars=256;
  static char least(String str) {
    int[] bro=new int[chars];
    int j;

    //I made the frequency of chars:
    for(j=0 ; j<str.length() ; j++) {
      (bro[str.charAt(j)])++; //made the array
    }
    int min= bro[str.charAt(0)];

    //Tried finding the smallest value:
    for(int x=0 ; x<bro.length ; x++ ) {
        if((bro[str.charAt(x)])<=min) {
            min=(bro[str.charAt(x)]); //finding the smallest number of times 
        }
    }
    return (char) min;
  }

  public static void main(String[] args) {
    String txt="yooooo bbaa ccoo";
    char rez=least(txt);
    System.out.println(rez);
  }
}

Upvotes: 0

Views: 113

Answers (5)

Zakaria Talhami
Zakaria Talhami

Reputation: 116

The Problem in your code is in:

bro[str.charAt(x)])

As the bro array is 256 long, x is also in the range of [0,256], but in your case you are indexing into the string str itself, which is not 256 long.

Also you are returning the min number of occurrences as a character, and not the character itself, in this case you have to return the key from the array not the value, in this case you need to return x.

Edited: You must also ignore all the character that don't appear at all in the string, in which case would always have a value of 0 and always be the min, despite not occurring.

Replace:

for(int x = 0; x < bro.length; x++) {
    if ((bro[str.charAt(x)]) <= min) {
        min = (bro[str.charAt(x)]); // finding the smallest number of times 
    }
}
return (char) min;

with:

for (int x = 0; x < bro.length; x++) {
    if ((bro[x]) <= min && bro[x] > 0) {
        min = x; // finding the smallest number of times 
    }
}
return (char) min;

Upvotes: 2

user1708042
user1708042

Reputation: 1863

I guess you wanted to print "y", and your code is broken in many ways:

this fixes it a bit:

public class Methods3 {
    static final int chars = Character.MAX_VALUE;   
    static char least(String str) {    
        int[] bro = new int[chars];    
        int j;    
        for (j = 0; j < str.length(); j++) {            
            (bro[str.charAt(j)])++; // made the array
        }
        int min = Character.MAX_VALUE;
        for (int x = 0; x < bro.length; x++) {
            if ((bro[x]) <= min && bro[x] > 0)  {
                min = x; // finding the smallest number of times
            }
        }

        return (char) min;
    }

    public static void main(String[] args) {    
        String txt = "yooooo bbaa ccoo";    
        char rez = least(txt);    
        System.out.println(rez);
    }
}

(fixed to use Character.MAX_VALUE as suggested). This will work with any string.

Please note that min must be initialized with the maximum value for your algorithm to work, hence min=Character.MAX_VALUE.

Where you used "bro[str.charAt(x)]" you had to use "bro[x]".

And in the if you need an extra condition "bro[x] > 0" to ignore chars you did not find.

Also if you have two letters with the same count it will print the last to appear in the string with the same minimum count.

Upvotes: 1

The Scientific Method
The Scientific Method

Reputation: 2436

String txt="yooooo bbaa ccoo";  // string length here

your string length index is in the range 0 <= x <= str.length -1 where as your for loop range is 0 <= x <= 255.

in the for loop, 0 <= x <= 255, see str.charAt(x)

if((bro[str.charAt(x)])<=min)

however,

str.length < 256  // that is the problem

x should be 0 <= x <= str.length -1

Upvotes: 0

wi2ard
wi2ard

Reputation: 1545

  • add a method to return the number of times the parameter char occurs in the string
  • initialise variable minOccurences with total number of chars
  • create empty list to store chars with minimum number of occurences
  • for each char in the string
    • call this method and get number of occurences
    • if minOccurences is greater than current number set it to current number, clear minChars and add the char
    • if minOccurences is equal to current number add the char

Upvotes: 0

Alexey  Usharovski
Alexey Usharovski

Reputation: 1442

Strings in Java are not one byte per symbol. Try to create bro array with length of Character.MAX_VALUE or use HashMap instead of array.

int[] bro=new int[Character.MAX_VALUE];

With Map

Map bro = new HashMap();
for(char cr : str.toCharArray()){
    bro.compute(cr, (key, val) -> val == null : 1 : val + 1);
}

Upvotes: 2

Related Questions