简体   繁体   中英

How do I change vowels in a string in Java if a certain condition is met?

How do I make it so that if the 3rd letter of the 1st string I input is found on the 2nd string, it will print the first string in all caps and change its vowels to *? If the 3rd letter is not found on the second string it will print the second string in small letters and replace all the vowels to @?

import java.io.*;
public class Program2 {
    
    public static void main(String args[]) throws IOException {
        BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the first String:");
        String first = data.readLine();
        System.out.println("Enter the second String:");
        String second = data.readLine();
        
        char a[] = first.toCharArray();
        char b[] = second.toCharArray();
        char c = first.charAt(2);
        char ch;
        String text;
        String mute;
        
        for (int w = 0; w < b.length; w++)
        {
            ch = second.charAt(w);
            if (ch == c) {                
                for (int i = 0; i < a.length; i++) {
                    if (a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u') {
                        a[i]='*'; //replaces the vowels with *
                        }            
                    text = Character.toString(a[i]);
                    System.out.print(text.toUpperCase());
            }
            }
            else if (ch != c) {
                for (int x = 0; x < b.length; x++) {
                    if (b[x]=='a'||b[x]=='e'||b[x]=='i'||b[x]=='o'||b[x]=='u') {
                        b[x]='@'; //replaces the vowels with @
                        }
                    mute = Character.toString(b[x]);
                    System.out.print(mute.toLowerCase());
                }
            }
        }
    }
}public static void main(String args[]) throws IOException {
        BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the first String:");
        String first = data.readLine();
        System.out.println("Enter the second String:");
        String second = data.readLine();
        
        char b[] = second.toCharArray();
        char c = first.charAt(2);
        char ch;
        String text;
        String mute;
        
        int x = 1;
        while (x < b.length) {
        {
            ch = second.charAt(x);
            if (ch == c) {                
                text = first.toUpperCase();
                System.out.println(text.replaceAll("[AaEeIiOoUu]", "*"));    
            }
            else if (ch != c) {
                mute = second.toLowerCase();
                System.out.println(mute.replaceAll("[AaEeIiOoUu]", "@"));
            }
        x++;    
    }
    }
}
}

somehow the code print multiple copies of the output and sometimes the second string appears even the second condition is not met

Your code prints out one string at each character of the second string, since it runs INSIDE the loop. So

  • get rid of the loop, String.indexOf(char) does what you want
  • do the string replacement outside the check to see if a character matches.

Something like:

...
if (second.indexOf(c)>-1){
   ... code for case when second string contains character
} else {
   ... code for case when second string does NOT contains character (indexOf returns -1)
}

Then, instead of using character arrays, investigate the String.replace function to simply replace some characters by others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM