I'm trying to take a line in single character input and if it's uppercase, switch it to lower case and vice versa.
System.out.print ("Enter character: ");
s=stdin.readLine();
c[i]=s.charAt(0);
k=(int)c[i];
if(k>96)
{
Character.toUpperCase(c[i]);
}
else
{
Character.toLowerCase(c[i]);
}
i++;
}
while(k!=46);
Everything else works right, but that doesn't flip it. HALP
<3
So in your mind, Character.toLowerCase magically knows that the single character you passed it is part of a String, and should actually find that particular String and perform the transformation in-place? I'd like to know how you arrived at this belief, because neither common sense nor the documentation support it.
>>5
I can't say I didn't deserve that. Once more, with feeling and proofreading: String cases = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c = "";
BufferedReader cin = new BufferedReader(new InputStreamReader(System.io));
int i = 0;
do
{
System.out.print("Enter character: ");
try
{
c = "" + (char)cin.read(); // Possible loss of precision?
}
throw(IOException e) { break; }
if((i = cases.indexOf(c)) != -1)
{
c = (i >= 26 ? c.toLowerCase() : c.toUpperCase());
System.out.println(c);
}
}
while(i != -1);
>>9
I was going to say that it could be an attempt at getting competent help by >>1, because while /prog/ hates helping people who don't deserve it, it hates getting a reputation for incompetence even more, and if someone posts really shit code in an attempt to help, someone else will usually come along to ridicule and correct; but then I noticed that it couldn't be >>1, because >>5-6's code doesn't suffer from >>1's specific problem.
Still, I'm sure it's only a matter of time before someone posts code that's as good as you can get while still writing Java.
public class SwapCase {
public static void main (String[] args) throws IOException {
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
char[] s = stdin.readLine().toCharArray();
for (int i = 0; i < s.length; ++i)
s[i] = Character.isLowerCase(s[i]) ? Character.toUpperCase(s[i])
: Character.toLowerCase(s[i]);
System.out.println(new String(s));
}
}
Lessons:
1. There is no meaningful way to recover from an IOException here, so just throw it further up.
2. Sun shits on your 80-character-width terminals.
3. Sepples programmers (>>5-6) are shit at Java too.
Name:
Anonymous2010-06-18 20:08
>>10
The easy compromise here is to write solutions only in the funges/fungoids: brainfuck, befunge, etc. In fact I have already made this my policy as of that counting thread.
>>3 I'd like to know how you arrived at this belief, because neither common sense nor the documentation support it.
common sense
common LISP ? (define-modify-macro character-to-upper-case () char-upcase)
CHARACTER-TO-UPPER-CASE
? (defvar *GRUNNUR* "sussman")
*GRUNNUR*
? (character-to-upper-case (char *GRUNNUR* 3))
#\S
? *GRUNNUR*
"susSman"
>>15,17
This is the true meaning of TIMTOWTDI. Not valid and interesting alternatively algorithms, but idiots making a huge mess out of simple problems in myriad ways.
Perl is a joke.