Reputation: 247
I have written a code that takes a string parameter and returns string that results from changing the capitalization of characters in the following way:
Here is my code:
public class Simple {
public char ChangeCase() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input String: ");
String inputString = scanner.nextLine();
//String isVowel = "aeiou";
char c='\0';
for (int i = 0; i < inputString.length(); i++) {
c = inputString.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') {
c = Character.toLowerCase(c);
System.out.println(c);
}
else if (c=='b'||c=='c'||c=='d'||c=='f'||c=='g'||c=='h'||c=='j'||c=='k'||c=='l'||c=='m'||c=='n'||c=='p'||c=='q'||c=='r'||c=='s'||c=='t'||c=='v'||c=='w'||c=='x'||c=='y'||c=='z'||
c=='B'||c=='C'||c=='D'||c=='F'||c=='G'||c=='H'||c=='J'||c=='K'||c=='L'||c=='M'||c=='N'||c=='P'||c=='Q'||c=='R'||c=='S'||c=='T'||c=='V'||c=='W'||c=='X'||c=='Y'||c=='Z'){
c = inputString.charAt(i);
c =Character.toUpperCase(c);
System.out.println(c);
}
else if(c=='@'||c=='!'||c=='"'||c==' '||c=='!'||c=='"'||c=='#'||c=='$'||c=='%'||c=='&'||c=='('|| c==')'||c=='*'||c=='+'||c==','||c=='-'||c=='.'||c=='/'||c==':'||c==';'||c=='<'||c=='='||c=='>'||c=='?'||c=='['||c==']'||c=='^'||c=='_'||c=='`'||c=='{'||c=='|'||c=='}'||c=='~'||c=='"'){
c=inputString.charAt(i);
c=c;
System.out.println(c);
}
else
c=c;
}
return c;
}
}
Runner class:
public class Runner {
public static void main(String[] args) {
Simple smpl=new Simple();
smpl.ChangeCase();
}
}
Results i get when I type hello:
Enter an input String: hello
H
e
L
L
o
Expected results:
HeLLo
What should I change to get the expected results?
Upvotes: 1
Views: 269
Reputation: 4094
change println()
to print()
to omit the linefeed character from the output. Swap toUpperCase
and toLowerCase
to make the code do what the specification requires.
You are also currently not printing other characters such as linefeed (the requirement "any characters that are not letters must not be changed").
Get rid of the second else if
and the else
branches and replace them with
else {
System.out.print(c);
}
Also get rid of the extra c = inputString.charAt(i);
statements. You only need the first one.
Regarding the problem you are solving, if I were you I would use regular expressions for that.
Anyway, here is a solution for Simple.java without regular expressions with some other improvements applied as well:
import java.util.Scanner;
public class Simple {
public void ChangeCase() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input String: ");
String inputString = scanner.nextLine();
String vowels = "aeiouAEIOU";
for (char c: inputString.toCharArray()) {
if (vowels.indexOf(c) != -1) { // found vowel
c = Character.toUpperCase(c);
} else if (Character.isLetter(c)) { // must be a consonant
c = Character.toLowerCase(c);
}
System.out.print(c);
}
}
}
Upvotes: 3
Reputation: 1441
First some hints for better code:
Here some simple examples for you (can be optimized):
public class Simple {
public void ChangeCase() {
String resultString;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input String: ");
String inputString = scanner.nextLine();
// make alle chars lower-case
resultString = inputString.toLowerCase();
// make vowels upper-case
resultString = uppercaseVowels(resultString);
// print the result or change to return String for main()
System.out.println("Input: " + inputString);
System.out.println("Result: " + resultString);
}
private String uppercaseVowels(String inputString) {
inputString = inputString.replace('a', 'A');
inputString = inputString.replace('e', 'E');
inputString = inputString.replace('i', 'I');
inputString = inputString.replace('o', 'O');
inputString = inputString.replace('u', 'U');
return inputString;
}
}
Upvotes: 0
Reputation: 541
Just use print() method instead of println() because println() always add new line.
Upvotes: 0
Reputation: 140523
As a first starter - simply swap
c = Character.toLowerCase(c);
respectively that other call that does toUpperCase()
within your if blocks.
Your code is basically correct - you are just changing cases in the wrong place. So simply "swap" these calls! And of course - don't use println()
- as this prints a "line" - meanning it adds a "new line" in the end.
Upvotes: 1
Reputation: 10628
Change println
to print
since println
will always add an extra row.
Upvotes: 3