Need help with some simple java stuff...

erifdekciw

New member
May 3, 2008
2,146
51
0
I never mess with java, but what I need seems like a pretty basic thing to do. Heres what I need help with.

I have a text box and enter button setup I just need it to display different results for a certain range.

For example lets say I want the person to enter results 1-100, but I only want to display 10 different results 1-10, 11-20, 21-30, etc.

Instead of coding it manually like if (e.keyCode == 1) then doing it again for 2 and so on. Is there a simple way to tell the code, if = 1-10 then go here instead of putting if code for every number?
 


not sure about java syntax exactly, but most languages you can just do a big switch:case statement in ascending order checking if the entered value is < than an increment of ten...


so it would be:

switch entered_value:
case entered_value < 10:
print some shit out;
case entered_value < 20:
print some other shit out;
 
You can probably do this in your event handler
// 1 - 10 case
if(e.keyCode => 1 || e.keyCode <= 10) {

}

or chain it like dchuk did
if(e.keyCode < 10) {
}
else
if(e.keyCode < 20) {
}

It's been a few years since I've coded but I remember that Java can only deal with switch statements using integer constants in the cases. Like 5 or Integer.MAX_INT. No expressions that need to be evaluated. It is or at least was very C like in that regard and
kind of archaic.
Anyway, that's why I used an if statement instead of switch() { case a: case b: }
 
Java switch can handle chars as well; if you are taking user input "1" (as a String), convert it to char equivalent of '1' , or convert it to an int.

Also, if you are using wrappers like Integer, you should cast it to primitive (int) before using it in a switch.
 
Oh yeah, thats right re constants in switch statements. Primitive/wrapper design is kind of ugly in Java.
I coded a shit-ton of java over a ten year period but barely touched it since before java 6 came out so I am pretty rusty.

I fired up eclipse and wrote a little scraper with htmlunit a few weeks ago. Pretty slick!
 
I never mess with java, but what I need seems like a pretty basic thing to do. Heres what I need help with.

I have a text box and enter button setup I just need it to display different results for a certain range.

For example lets say I want the person to enter results 1-100, but I only want to display 10 different results 1-10, 11-20, 21-30, etc.

Instead of coding it manually like if (e.keyCode == 1) then doing it again for 2 and so on. Is there a simple way to tell the code, if = 1-10 then go here instead of putting if code for every number?

Java...it has been a while. Will this work?


if ( ((int)(number -1/10)) == 1){
goToresult1();
}else if ( ((int)(number -1/10)) == 2){{
goToresult2();
}
etc

(How the hell do I highlight syntax?!)