-4

I'm trying to make a program which splits strings, and returns the strings with 3 or more vowels and i keep getting an array out of bound exception. i dont know how to return an array at the end of the method.

Where is the problem in my program which i apparently cant see?

public class SplitWords {

    public static void main(String [] args){
        Scanner scan =  new Scanner(System.in);

        final int MIN_SIZE = 4;
        int size = 0;

        do{

            System.out.println("How many words are you typing(min " + MIN_SIZE + "): ");
            size = scan.nextInt();
        }while(size < MIN_SIZE);
        scan.nextLine();

        String[] myarray = new String[size];
        for(int i = 0; i < myarray.length; i++){
            System.out.println("Type the sentence in the position " + i);
            myarray[i] = scan.nextLine();
        }

        System.out.println("The words which have 3 or more vowels are: " + findVowels(myarray));
    }

    public static String findVowels(String[] myarray){
    for(int i = 0; i < myarray.length; i++){
        String[] tokens = myarray[i].split(" ");
                int count = 0;
                for(int j = 0; j < myarray.length; j++) {
                    if(isVowel(tokens[i].charAt(j))){
                           count++;                     
                    }
                 }
                 if(count > 3){
                  break;
                 }
    }
        return null;
    }

    public static boolean isVowel(char ch){
            switch(ch){
                case 'a':
                    case'e':
                        case'i':
                            case'o':
                                case'u':
                                    case'y':
                                        return true;
            }
            return false;
    }
} 
JaneSus
  • 1
  • 2

2 Answers2

0

Why are you splitting the string into tokens? Also calling String[], I would guess you either meant char[] or String, because what you are doing is creating an array of strings and acting like the array of strings is a single string. Just use "String" not "String[]"

  • im very sorry guys, im a beginner first year of uni, and this was an assigment and its gonna help me on my exam – JaneSus Sep 15 '17 at 21:34
0

This is the problem

 for(int j = 0; j < myarray.length; j++) {
                    if(isVowel(tokens[i].charAt(j))){
                           count++;                     
                    }
                 }

When you have split the string

 String[] tokens = myarray[i].split(" ");

Why don't you use tokens.length instead of myarray.length, and use tokens[j] not i, i is the counter for number of strings you have.

After integrating the above changes, your code should look like this

public static String findVowels(String[] myarray){

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

            String[] tokens = myarray[i].split(" ");
                    int count = 0;
                    for(int j = 0; j < tokens.length; j++) {

                        String str = tokens[j];

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

                            if(isVowel(str.charAt(j2))){
                                   count++;                     
                            }
                             if(count > 3){
                              break;
                             }
                        }

                     }

        }
            return null;
        }

This doesn't give any exception but I am quite surprised with the logic of this code as you are returning null at the end of the method no matter what.

Isha Agarwal
  • 420
  • 4
  • 12
  • i did what you said and it returned null – JaneSus Sep 15 '17 at 21:33
  • Your code contains lot of bugs, I have fixed the method which was causing problem, tokens[j] is a string and you need to iterate that string to check each character, you did something wrong in your code – Isha Agarwal Sep 15 '17 at 22:02
  • I didnt know how to return the array with the splitted words in the end thats the problem – JaneSus Sep 16 '17 at 05:42