Home > Back-end >  Vowel substring
Vowel substring

Time:11-02

I have to write a program that takes a String as user input and then prints a substring that starts with the first vowel of the String and ends with the last. So for instance if my String is : "Hi I have a dog named Patch", the printed substring would be : "i I have a dog named Pa"

This is the code I have now:

import java.util.Scanner;
import java.util.*;
public class SousChaineVoyelle {
    private static Scanner sc;
    public static void main (String[] args) {
        
        sc = new Scanner(System.in);
        
        System.out.print("Enter a String: ");
        String str = sc.nextLine();
        
        int pos1 = 0;
        int pos2 = 0;
        int i;
        int j;
        
        boolean isVowel1 = false;
        
        boolean isVowel2 = false;
        
        
        for (i = 0; i < str.length(); i  ){
            if (str.charAt(i) == 'A' || str.charAt(i) == 'a' || 
            chaine.charAt(i) == 'E' || str.charAt(i) == 'e' || 
            str.charAt(i) == 'I' || str.charAt(i) == 'i' || 
            str.charAt(i) == 'O' || str.charAt(i) == 'o' || 
            str.charAt(i) == 'U' || str.charAt(i) == 'u' || 
            str.charAt(i) == 'Y' || str.charAt(i) == 'y'){
            isVowel1 = true;
            break;
            }           
        }
        if (isVowel1){
            pos1 = str.charAt(i);
        }
        
        for (j = str.length() - 1; j > i; j--){
            if (str.charAt(j) == 'A' || str.charAt(j) == 'a' || 
            str.charAt(j) == 'E' || str.charAt(j) == 'e' || 
            str.charAt(j) == 'I' || str.charAt(j) == 'i' || 
            str.charAt(j) == 'O' || str.charAt(j) == 'o' || 
            str.charAt(j) == 'U' || str.charAt(j) == 'u' || 
            str.charAt(j) == 'Y' || str.charAt(j) == 'y'){
            isVowel2 = true;
            break;
            }
        }
        if (isVowel2){
                pos2 = str.charAt(j);
        }
        
        
                
        String sub = chaine.substring(pos1, pos2);
        
        System.out.print(The substring from the first vowel to the last is  "\""   sub  "\"");
    }
}

this got me this: 


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 22 out of bounds for length 22 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4557) at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:46) at java.base/java.lang.String.charAt(String.java:1515) at SousChaineVoyelle.main(SousChaineVoyelle.java:33)

CodePudding user response:

One way of looking at it is you have a lot of code to do a simple thing, which means more chances for bugs and errors.

Here's a "less code" solution:

str = str.replaceAll("(?i)^[^aeiou]*|[^aeiou]*$", "");

See live demo.

This works by matching all leading and trailing non-vowels (if any) and replacing them with nothing, effectively deleting them.
(?i) makes the match case insensitive.

CodePudding user response:

After the first for loop, i will be str.length() no matter what. You may want to create another variable to hold I when it's found to remedy this.

  • Related