javip
javip

Reputation: 259

How to replace vowels in a char array

I'm having trouble creating an array that will change the vowels in my array with what a user inputs. An issue that has come up is that when it asks for letter i I get an inputmismatch.

/******************************************************************************
 * This function will prompt the user to replace all vowels in the array
 ******************************************************************************/
public static void replace( char [] letters )
{
    Scanner scan = new Scanner(System.in);
    System.out.print(" Enter a character for i: ");
    int S = scan.nextInt();

    for(int i = 0; i < letters.length; i++)
    {
        if(letters[i] == 'A' || letters[i] == 'E' || letters[i] == 'I')
        {
            letters[i]= (char)S;
            System.out.print(letters);
        }
    }
}

Upvotes: 0

Views: 805

Answers (2)

Reimeus
Reimeus

Reputation: 159844

To read a single character from the input Scanner you can simply use:

char s = scan.next().charAt(0);

or

char s = scan.findInLine(".").charAt(0);

if you just wish to consume a single character.

Upvotes: 1

Kailua Bum
Kailua Bum

Reputation: 1368

printing an array is not the same as printing a primitave data type. what you have

System.out.print(letters);

you can print the array by iterating through it

for(int k =0;k < letters.length;k++){   
    System.out.println(letters[k]);
}

also the scanner is asking the user for a number, it seems you really want them to enter a letter

Upvotes: 2

Related Questions