Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java help (I know, I know java sux lol)

Name: Anonymous 2010-06-18 17:28

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

Name: Anonymous 2010-06-18 17:30

I swear to god, I'm going to start murdering people if this sort of retard shit keeps getting posted

Name: Anonymous 2010-06-18 17:31

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.

No wonder you're using Java.

Name: Anonymous 2010-06-18 17:43

[code] tags.

Name: Anonymous 2010-06-18 18:56

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);

Name: Anonymous 2010-06-18 18:59

>>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);

Name: Anonymous 2010-06-18 19:04

print 'uSE A REAL LANGUAGE.'.swapcase()

Name: Anonymous 2010-06-18 19:06

>>5-6
Java is a horrible language no matter how you look at it, but I bet people would hate it less if its users weren't so goddamn incompetent.

Name: Anonymous 2010-06-18 19:18

>>5-6
I no longer have enough faith in humanity left to believe that this is a deliberate troll. :(

Name: Anonymous 2010-06-18 19:26

>>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.

Name: Anonymous 2010-06-18 20:01

>>10
IHBT :(

This is where this thread is going anyway, so:

import java.io.*;

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: Anonymous 2010-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.

Name: Anonymous 2010-06-18 20:34

>>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"

Name: Anonymous 2010-06-18 20:37

CL one-liner:
(map 'string #'(lambda (c) (if (lower-case-p c) (char-upcase c) (char-downcase c))) (read))

Name: Anonymous 2010-06-19 12:07

Have some line noise
print for map{$_=substr($s,$_,1);$_=~/[A-Z]/&&lc$_||uc$_}(0..length($s))

Name: Anonymous 2010-06-19 12:17

<?php
isset($_SERVER['argv'][1]) ? $s = $_SERVER['argv'][1] : die();
foreach(str_split($s) as $c) { echo strtoupper($c) != $c ? strtoupper($c) : strtolower($c); }

Name: Anonymous 2010-06-19 13:37

>>15
What the fuck is this shit?
Perhaps you mena: s/(\p{Ll})|(\p{Lu})/\U$1\L$2/g

Name: Anonymous 2010-06-19 14:27

>>15,17
tr/a-zA-Z/A-Za-z/;

Twits.

Name: Anonymous 2010-06-19 14:39

>>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.

Name: Anonymous 2010-06-19 14:58

char s[] = "Stupid script kiddies", *p;
for (p = s; *p; p++) if (isalpha(*p)) *p ^= 20;
puts(s);

Name: Anonymous 2010-06-19 15:23

>>20
QLDQF@ DF[SFUYYQF

Name: Anonymous 2010-06-19 15:29

>>21
[[DG \U\U\U
obviously should have been ^= 0x20

Name: Anonymous 2010-06-19 18:51

>>18
Way to fail on non-english characters.

Name: Anonymous 2010-06-19 19:37

tr/[:lower:][:upper:]/[:upper:][:lower:]/
POSIX saves the day again

Name: Anonymous 2010-06-19 20:04

>>24
What language is that?

Name: Anonymous 2010-06-19 20:46

>>25
GNU/Perl.

Name: Anonymous 2010-06-19 20:47

>>26
Does Perl really let you mean that?

Name: Anonymous 2010-06-19 20:50

>>27
Of course not. POSIX tr does, though.

Name: Anonymous 2010-06-19 21:30

>>28
I knew I'd be surprised either way.

Name: Anonymous 2010-06-20 4:49

>>29
bash-3.2$ perl -e '$foo="Hello";$foo=~tr/A-Za-z/a-zA-Z/;print $foo."\n";'
hELLO

Name: Anonymous 2010-06-20 5:35

>>28
Does Anoncoreutils tr do it, too?

Name: Anonymous 2010-06-20 5:58

>>30
Jesus, learn to use perl already. And learn to write Perl while you're at it.

perl -p -e 'tr/A-Za-z/a-zA-z/'

Name: Anonymous 2010-06-20 7:13

>>32
Not only that, but yours is erroneous. At least >>30 gave evidence that it worked.

Name: Anonymous 2010-06-20 8:06

>>30
Why did you post this?  Stop regressing, assholes.

Name: Anonymous 2013-01-19 23:39

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List