2016-08-29 07:27:53 -06:00
|
|
|
// validating/Inverter4.java
|
2016-08-19 08:21:09 -06:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-08-29 07:27:53 -06:00
|
|
|
package validating;
|
2016-08-19 08:21:09 -06:00
|
|
|
import static java.lang.Character.*;
|
|
|
|
|
2016-08-29 07:27:53 -06:00
|
|
|
public class Inverter4 implements StringInverter {
|
2016-11-21 12:37:57 -08:00
|
|
|
static final String ALLOWED =
|
2016-08-19 08:21:09 -06:00
|
|
|
"abcdefghijklmnopqrstuvwxyz ,." +
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
public String invert(String str) {
|
2016-08-28 06:13:56 -06:00
|
|
|
if(str.length() > 30)
|
2016-08-19 08:21:09 -06:00
|
|
|
throw new RuntimeException("argument too long!");
|
|
|
|
String result = "";
|
|
|
|
for(int i = 0; i < str.length(); i++) {
|
|
|
|
char c = str.charAt(i);
|
2016-11-21 12:37:57 -08:00
|
|
|
if(ALLOWED.indexOf(c) == -1)
|
2016-08-19 08:21:09 -06:00
|
|
|
throw new RuntimeException(c + " Not allowed");
|
|
|
|
result += isUpperCase(c) ?
|
|
|
|
toLowerCase(c) :
|
|
|
|
toUpperCase(c);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|