susie y
susie y

Reputation: 31

Replace each vowel in string with following vowel using switch statement

Im new to programming and learning java. I am struggling to get this switch statement to work with replacing vowels in a string and then printing the new string. I keep getting cannot find symbol with ChangeV. Also, unsure how I can iterate through my string to check for vowels and then replace each vowel. I would like to take input from user but again not clear on how to incorporate properly. Thanks for any help!

public class Main{

    public static void main(String args[]){


    //Scanner myScanner = new Scanner(System.in);
    //String s = myScanner.next();
    ChangeV v = new ChangeV();

    System.out.println(v.ChangeV());

}


public void ChangeV(){


String s = "aeiou";
char [] vowels ={'a', 'e' ,'i', 'o','u'};

// I believe I would need a for loop 

switch (//not sure what goes here) {
    case'a' :

    s.replace('a', 'e');

    break;

    case 'e' :

    s.replace('e', 'i');


    break;

    case 'i' :

    s.replace('i', 'o');


    break;

    case 'o' :

    s.replace('o', 'u');

    break;


    case 'u' :

    s.replace('u', 'a');


    break;
}

}
    }

}

Upvotes: 1

Views: 1461

Answers (3)

duffymo
duffymo

Reputation: 308928

You wrote this line:

ChangeV v = new ChangeV();

You're telling the compiler that there's a class named ChangeV with a no-argument constructor.

I see no such class in your code, only public class Main.

You also wrote this line:

public void ChangeV(){

This is incorrect because it falls outside the braces in the Main class. Java is an object-oriented language. Every function has to appear inside a class definition. Languages like Python are different: You can define a function on its own in a source file. Not so in Java.

If it were inside the braces for Main it would be a method on the Main class named ChangeV that takes no arguments and returns void.

That is not a constructor.

A constructor is a special method that has the same name as the class it's part of and no return type.

This is a constructor with no arguments:

public ChangeV() {
    // initialize the instance in here.
}

I don't think you want that.

Here's what I think you want:

public class VowelChanger {

    public static void main(String [] args) {
        for (String arg : args) {
            System.out.println(String.format("before: %s after: %s", arg, changeVowels(args));
        }
    }

    public static String changeVowels(String s) {
        // put your code to replace vowels here.
    }
}

Upvotes: 1

AxelH
AxelH

Reputation: 14572

I believe duffymo's answer already covert the problem of your method usage so I will simply mention the problem to use replace like s = s.replace('a', 'e');

String.replace will replace every occurence, so you could simply call it 5 times in a row :

s = s.replace( 'a', 'e' );
s = s.replace( 'e', 'i' );
s = s.replace( 'i', 'o' );
s = s.replace( 'o', 'u' );
s = s.replace( 'u', 'a' );

But you will end up with only 'a' since every 'a' will become 'e', then every 'e' 'i' and so one until every 'u' become 'a'. Ok, we could reverse the ordre to look for vowels after there where modify :

s = s.replace( 'u', 'a' );
s = s.replace( 'o', 'u' );
s = s.replace( 'i', 'o' );
s = s.replace( 'e', 'i' );
s = s.replace( 'a', 'e' );

But we still have a problem with the 'u' that end up in 'e' (temporarily 'a'). So a workaround would be to use a temporary character but how to be sure it is not already used in your text :

s = s.replace( 'u', '*' ); //don't set it to 'a' directly
s = s.replace( 'o', 'u' );
s = s.replace( 'i', 'o' );
s = s.replace( 'e', 'i' );
s = s.replace( 'a', 'e' );
s = s.replace( '*', 'a' ); //then replace the temp character by 'a'

A better solution would be to treat the text, one character at the time. If you find a vowel, you change it.

My solution would use an array to reduce the verbosity and let the index give you the next vowels. (using a % array.length to get back at the beginning if you are after the last vowel)

static char [] vowels ={'a', 'e' ,'i', 'o','u'};

public static String changeV(String s){
    char[] chars = s.toCharArray(); //transform a String in an array of character
    for ( int i = 0; i < chars.length; ++i ) { //iterate the array
        int index = Arrays.binarySearch( vowels, chars[i] ); //look for the current character in the vowels array and get his index
        if ( index > -1 ) // is it a vowels
            chars[i] = vowels[( index + 1 ) % vowels.length]; // change with the next vowels
    }
    return new String(chars); //create a nez String with the result
}

This would give you :

changeV( "aeiou" ); //eioua
changeV( "This is a test" ); //Thos os e tist
changeV( "Hello world" ); //Hillu wurld

Upvotes: 0

Madhu Bhat
Madhu Bhat

Reputation: 15213

There are two issues with the way you are approaching.

  1. s.replace('a', 'e'); will not replace any character in the string since String object is immutable in Java. It would only replace if you assign it back to s as s = s.replace('a', 'e');
  2. The problem with replacing every character of a string in a loop in this way is, on every next iteration, the replace would even replace any past characters if it matches the character. So essentially, "aeiou" would become "aaaaa" at the end since at the last character, you would have s = s.replace('u', 'a'); which would replace every character with a.

So to replace characters within a string, below is a better approach:

public static void main(String[] args){

    String oldString = "aeiou";
    char[] characters = oldString.toCharArray();

    for (int i = 0; i < characters.length; i++) {
        switch (characters[i]) {
            case 'a':
                characters[i] = 'e';
                break;
            case 'e':
                characters[i] = 'i';
                break;
            case 'i':
                characters[i] = 'o';
                break;
            case 'o':
                characters[i] = 'u';
                break;
            case 'u':
                characters[i] = 'a';
                break;
            default:
                break;
        }
    }

    String newString = String.valueOf(characters);
    System.out.println(oldString);
    System.out.println(newString);
}

In case you want to replace the vowels with any string input by user, you can use Scanner to read the input from user via console and it can be implemented as below.

public class Main {

    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);
        String oldString = scanner.next();

        String newString = changeVowels(oldString);

        System.out.println(newString);
        scanner.close();

    }

    private static String changeVowels(String oldString) {
        char[] characters = oldString.toCharArray();

        for (int i = 0; i < characters.length; i++) {
            switch (characters[i]) {
                case 'a':
                    characters[i] = 'e';
                    break;
                case 'e':
                    characters[i] = 'i';
                    break;
                case 'i':
                    characters[i] = 'o';
                    break;
                case 'o':
                    characters[i] = 'u';
                    break;
                case 'u':
                    characters[i] = 'a';
                    break;
                default:
                    break;
            }
        }

        return String.valueOf(characters);
    }
}

Upvotes: 2

Related Questions