-3

Question is to convert all consonants in given string to @ here I have used string builder to take input but while convertion all characters in string get converted to @ why is that please help?

import java.util.Scanner;

public class Sample 
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder str2= new StringBuilder(sc.nextLine());
        
        for(int i=0;i<str2.length();i++)
        {
            if(str2.charAt(i)!='a'||str2.charAt(i)!='e'||str2.charAt(i)!='i'||str2.charAt(i)!='o'||str2.charAt(i)!='u'||str2.charAt(i)!='A'||str2.charAt(i)!='E'||str2.charAt(i)!='I'||str2.charAt(i)!='O'||str2.charAt(i)!='U'||str2.charAt(i)!=' ')   
            {
                str2.setCharAt(i,'@');
            }
        }
        System.out.println(str2);
    }

}

sample input - aacaaaa Expected output - aa@aaaa

output from above program - @@@@@@@

krish777
  • 5
  • 2
  • `str2.charAt(i)!='a'||str2.charAt(i)!='e'` will *always* be true, because a character is always either not 'a' or not 'e' – QBrute Sep 18 '21 at 13:06
  • @JohnnyMopp doing this will replace all vowels though. – QBrute Sep 18 '21 at 13:11
  • @QBrute You are correct. I misread it - thought they wanted to replace vowels. – 001 Sep 18 '21 at 13:16
  • Here's a simpler condition: `if ("aeiou".indexOf(Character.toLowerCase(str2.charAt(i))) == -1)` – 001 Sep 18 '21 at 13:25

3 Answers3

1

you should use && insetead of ||

if (str2.charAt(i) != 'a' && str2.charAt(i) != 'e' && str2.charAt(i) != 'i' && str2.charAt(i) != 'o' && str2.charAt(i) != 'u' && str2.charAt(i) != 'A' && str2.charAt(i) != 'E' && str2.charAt(i) != 'I' && str2.charAt(i) != 'O' && str2.charAt(i) != 'U' && str2.charAt(i) != ' ') {


} 
DEV
  • 1,607
  • 6
  • 19
1

You should be using && instead of ||. But I think the better way to implement this would as follow

import java.util.Scanner;

public class Sample 
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringBuilder str2= new StringBuilder(sc.nextLine());
        String vowels = "aeiouAEIOU";
        for(int i=0;i<str2.length();i++)
        {
            if(vowels.indexOf(str2.charAt(i)) == -1 || str2.charAt(i) == ' ')   
            {
                str2.setCharAt(i,'@');
            }
        }
        System.out.println(str2);
    }

}
K450
  • 691
  • 5
  • 17
0

Try this out, putting all the vowels into a list makes your code much easier to read.

import java.util.Arrays;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the string to check");
        String checkString = in.nextLine();
        StringBuilder builder = new StringBuilder();
        String[] vowels = new String[] { "a", "e", "i", "o", "u" };

        if (checkString != ""){
            String[] check = checkString.split("");
            for (String c : check){
                if (!Arrays.asList(vowels).contains(c.toLowerCase()) || c.equals(" ")){
                    builder.append("@");
                } else {
                    builder.append(c);
                }
            }
        }
        System.out.println(builder);
    }
}
Wellerman
  • 846
  • 4
  • 14