EverCrest Message Forums
You are not logged in. Login or Register.
Author
Topic: Java Help needed :(
Khyron
Hello, my mushy friend...
posted 11-23-2003 11:40:37 PM
I know the answer to this! But I'm having a severe brainfart.

Is the code for a case statement like this?

code:

switch (x)
{
case 1 :
y = 10;
do;

case 2 :
y = 5;
do;
}


I honestly can't remember the syntax, and I left my notes at work :cry:

Snugglits
I LIKE TO ABUSE THE ALERT MOD BUTTON AND I ENJOY THE FLAVOR OF SWEET SWEET COCK.
posted 11-23-2003 11:41:51 PM
Do? Please describe what it is you're 'do'ing.

If you mean switches in general, you don't need the "do;". You would need a "break;" if you don't want case 1 to run through case 1, case 2, etc.

code:
 switch(x)
{
case 1: y = 10;
break;
case 2: y = 5;
break;
default: System.out.println("Pikachu!");
break; //not really necessary but advocated by teachers anyway
}

[ 11-23-2003: Message edited by: Oscar 3/Xray 1 ]

[b].sig removed by Mr. Parcelan[/b]
Khyron
Hello, my mushy friend...
posted 11-23-2003 11:47:36 PM
I dunno, Waisz, I honestly dunno Like I said, suddenly my brain seized up and I couldn't remember the syntax =/
Snugglits
I LIKE TO ABUSE THE ALERT MOD BUTTON AND I ENJOY THE FLAVOR OF SWEET SWEET COCK.
posted 11-23-2003 11:52:37 PM
quote:
Verily, Khyron doth proclaim:
I dunno, Waisz, I honestly dunno Like I said, suddenly my brain seized up and I couldn't remember the syntax =/

I meant, were you using a normal switch or a switch with some kind of loop?

[b].sig removed by Mr. Parcelan[/b]
Khyron
Hello, my mushy friend...
posted 11-23-2003 11:54:10 PM
quote:
We were all impressed when Oscar 3/Xray 1 wrote:
I meant, were you using a normal switch or a switch with some kind of loop?

This is the exact code I was doing...

code:
        switch (month)
{
case 1 :
mth = "Jan";
break;

case 2 :
mth = "Feb";
break;

case 3 :
mth = "Mar"
break;

case 4 :
mth = "Apr";
break;

case 5 :
mth = "May";
break;

case 6 :
mth = "Jun";
break;

case 7 :
mth = "Jul";
break;

case 8 :
mth = "Aug";
break;

case 9 :
mth = "Sep";
break;

case 10 :
mth = "Oct";
break;

case 11 :
mth = "Nov";
break;

case 12 :
mth = "Dec";
break;
}

I just had a brainfart and I started mixing Pascal syntax and java syntax I had case (month) at first and wondered why it wouldn't compile... then I realised I couldn't remember what next.

[ 11-23-2003: Message edited by: Khyron ]

Nina
posted 11-24-2003 12:09:48 AM
... use an array?

String monthArray[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
mth = monthArray[month-1];

[ 11-24-2003: Message edited by: Nina ]

Snugglits
I LIKE TO ABUSE THE ALERT MOD BUTTON AND I ENJOY THE FLAVOR OF SWEET SWEET COCK.
posted 11-24-2003 12:13:33 AM
quote:
Nina had this to say about (_|_):
... use an array?

String monthArray[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
mth = monthArray[month-1];


That works, too, unless this is going in a class with a lot of instances where having the string array would cause too much memory usage (on computers that are over a decade old or something ).

Oh, nevermind that, monthArray[] would be a static final.

[ 11-24-2003: Message edited by: Oscar 3/Xray 1 ]

[b].sig removed by Mr. Parcelan[/b]
Demos
Pancake
posted 11-24-2003 12:33:13 AM
Yeah Khy, your 2nd one was correct for the situation. I'd just recommend something for the default case - just to eliminate a very small chance of bad data. Especially if you're doing any manipulation of the data, you don't want to end up dividing by zero.
"Jesus saves, Buddha enlightens, Cthulhu thinks you'll make a nice sandwich."
Naimah
In a Fire
posted 11-24-2003 01:16:26 AM
Or just set up a mutator. Because then if you screw it up it's your own damn fault and deserve to get the runtime error.
Khyron
Hello, my mushy friend...
posted 11-24-2003 01:47:02 AM
Unfortunately, we can't do arrays yet.

Well, I CAN do an array, but since the teacher hasn't instructed us on them yet, then I would probably lose points on the assignment if I did so.

Nina
posted 11-24-2003 02:03:18 AM
If my teacher took away points for doing more than he asks for, I'd be failing my class right now... =x
Rabidbunnylover
Pancake
posted 11-24-2003 06:55:58 PM
mth = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format((new java.util.GregorianCalendar(2003, month - 1, 1)).getTime( )).substring(0, 3);

[ 11-24-2003: Message edited by: Rabidbunnylover'nix ]

Merp
diadem
eet bugz
posted 11-24-2003 07:11:35 PM
Khyron, you may want to use enums or at least constants for constancy’s sake. Even for months, magic numbers are bad...


so

code:

case MONTH.Oct :
mth = "Oct";
break;

play da best song in da world or me eet your soul
diadem
eet bugz
posted 11-24-2003 07:14:52 PM
quote:
Rabidbunnylover'nix had this to say about Cuba:
mth = java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).format((new java.util.GregorianCalendar(2003, month - 1, 1)).getTime( )).substring(0, 3);


lol

you code like i did when I was in college. an entire assignment in as little code as possible using only one line. i enjoyed doing that.. handing in stuff seconds after it was assigned. good man for that but... i believe the point of the assignment was to learn how to use select case correctly. and the professors hate people doing what we do. I remember during a final getting a problem marked wrong (i wrote the code on the paper with some short recursive statement) and didn't get it fixed until I insisted the prof actually enter the code into a computer and try it out.. it worked, he was a little surprised, but I got the problem marked right. how I programmed in college and how I program the "proper" way in the real world are... worlds apart.

[ 11-24-2003: Message edited by: diadem ]

play da best song in da world or me eet your soul
Y.O.T.C
No longer a Towel Girl
posted 11-24-2003 09:27:23 PM
I'm in the same java class as Waisz, MorbId and Demos and this is my input ont he subject.

"... ... ... My brain hurts from the stupidness of our teacher."

Yes, that is my input. Also I agree with what everyone else here says. Of course you could just have it go through a gheto if loop or a gheto for loop till it finds the number its looking for then output said month. Th efor loop would suck btw. alot.

Rabidbunnylover
Pancake
posted 11-25-2003 08:45:24 AM
quote:
diadem stopped staring at Deedlit long enough to write:
lol

you code like i did when I was in college. an entire assignment in as little code as possible using only one line. i enjoyed doing that.. handing in stuff seconds after it was assigned. good man for that but... i believe the point of the assignment was to learn how to use select case correctly. and the professors hate people doing what we do. I remember during a final getting a problem marked wrong (i wrote the code on the paper with some short recursive statement) and didn't get it fixed until I insisted the prof actually enter the code into a computer and try it out.. it worked, he was a little surprised, but I got the problem marked right. how I programmed in college and how I program the "proper" way in the real world are... worlds apart.


Yeah...my comp. sci. teacher at the moment used to work at IBM, and he's big on programming style. So I've learned how to whitespace nicely and follow style guides, but it's still fun to bastardize the language from time to time.

Merp
Naimah
In a Fire
posted 11-25-2003 11:20:41 AM
I've been thinking about doing an entire program in the prepend section of a for loop just to give the graders a headache. I hate my CS class by the way.
All times are US/Eastern
Hop To: