method to return a String depending on various cases

M

Matt

I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.


Please advise. thanks!!
 
J

Joona I Palaste

Matt said:
I have a method to return a String depending on the random number
public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}
If I omit line return "", it will have compile error "This method must
return the result of type String"
I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.
Please advise. thanks!!

Not tested, but try inserting this line inside your switch:

default: return "";

The reason for the error is that javac isn't sentient, and so it can't
"know" that the n = n%3 will always cause either case 0, 1 or 2 to be
executed.
 
E

Eric Sosman

Matt said:
I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

Rewrite the `switch' slightly:

switch(n)
{ default: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}

Or, if you prefer:

switch(n)
{
default:
case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
 
C

Carl Howells

Matt said:
I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

In that type of situation, when other approaches (like those suggested
in the other replies) won't work, I find that instead of a dummy return,
a dummy throw is better.

For instance, instead of:

return "";

use

throw new IllegalStateException("This should have been unreachable.");
 
G

Grant Wagner

Matt said:
I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

Please advise. thanks!!

Don't use a switch at all and avoid the problem:

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
return [ "EXACTMATCH", "RANGEMATCH", "ALTMATCH"][n];
}

or:

public String getResponseType()
{
return [ "EXACTMATCH", "RANGEMATCH", "ALTMATCH"][Math.abs((new
Random()).nextInt()) % 3];
}
 
B

Bryce

I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

What does it return if n = 4. Yea, given the mod above it can't, but I
don't know if the compiler knows.

What I do is this:

public String getResponseType()
{
String result = null;
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: result = "EXACTMATCH";
case 1: result = "RANGEMATCH";
case 2: result = "ALTMATCH";
}
return result;
}

I do that because I don't like having return statements all over my
code. Just a personal preference.
 
T

Tony Morris

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

Throw a java.lang.IllegalStateException instead.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
T

Thomas Weidenfeller

Matt said:
I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

private static String[] responseTypes = {
"EXACTMATCH", "RANGEMATCH", "ALTMATCH"
}

private static Random r = new Random();

public String getResponseType()
{
int n = Math.abs(r.nextInt());
return responseTypes[n % responseTypes.length()];
}

/Thomas
 
S

Simon

I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
default: return "ALTMATCH"; // case 2
}
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

You need to use the keyword 'default'. Your function declares that it
will return a String from every single execution path. If you use
only cases without a default, it will assume there may be a case that
you didn't account for, subsequently fall through to the next line
outside the switch statement and execute that line instead. By using
default, you are ensuring that every single execution path is now
handled by the switch statement, which relinquishes you from the need
to use any "unreachable" code afterwards. In fact, now if you were to
leave the 'return "";' statement at the bottom, the compiler should
complain that the code is in fact unreachable and you will be forced
to remove it. I hope my explaination helps more than just giving you
the array indices solution (which is much better).
 
S

Simon

I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
default: return "ALTMATCH"; // case 2
}
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

You need to use the keyword 'default'. Your function declares that it
will return a String from every single execution path. If you use
only cases without a default, it will assume there may be a case that
you didn't account for, subsequently fall through to the next line
outside the switch statement and execute that line instead. By using
default, you are ensuring that every single execution path is now
handled by the switch statement, which relinquishes you from the need
to use any "unreachable" code afterwards. Now, if you were to
leave the 'return "";' statement at the bottom, the compiler should
complain that the code is in fact unreachable and you will be forced
to remove it. I hope my explaination helps more than just giving you
the array indices solution (which is much better).
 
C

Chris Smith

Tony said:
Throw a java.lang.IllegalStateException instead.

This is a great idea in general (certainly better than just returning
incorrect values). Just curious, though -- two people now have
suggested throwing IllegalStateException. Yet there is no illegal
state. In fact, as near as I can tell, this method makes no use of
state at all. IllegalStateException implies that the method could be
called if the object were in a different state and it would succeed.

If there's no better exception and the condition is entirely impossible
to begin with, it seems far preferable to me to just throw
RuntimeException instead.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
W

Will

Matt said:
If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.

If you want the final 'return' statement to be reachable, you could assign
values to a string variable and return that at the end of the method:

public String getResponseType()
{

Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
String responseType = "";

switch(n)
{ case 0: responseType = "EXACTMATCH";
case 1: responseType = "RANGEMATCH";
case 2: responseType = "ALTMATCH";
}
return responseType;
}

Regards, Will.
 
K

Kevin McMurtrie

I have a method to return a String depending on the random number

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
}
return "";
}

If I omit line return "", it will have compile error "This method must
return the result of type String"

I feel uncomforable with the line return ""; because this line is
always unreachable, but if
I omit this, I will have compile error.


Please advise. thanks!!

public String getResponseType()
{
Random r = new Random();
int n = r.nextInt();
n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
default: throw new Error ();
}
}
 
R

Roedy Green

n = Math.abs(n);
n = n % 3;
switch(n)
{ case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
default: throw new Error ();

The compiler does not know the only possible values of n are 0, 1 and
2. you could throw in an

for the default case.
assert false: "modulus is broken";
 
T

Tony Morris

The compiler does not know the only possible values of n are 0, 1 and
2. you could throw in an

for the default case.
assert false: "modulus is broken";

That's an abuse of the purpose of assert.
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

Better would be to throw a java.lang.IllegalStateException() - not an Error.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
K

Kevin McMurtrie

The compiler does not know the only possible values of n are 0, 1 and
2. you could throw in an

for the default case.
assert false: "modulus is broken";

That's an abuse of the purpose of assert.
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

Better would be to throw a java.lang.IllegalStateException() - not an Error.[/QUOTE]

I picked Error because it's less likely to be ignored during testing and
it can not be thrown because of bad input or use. It can only be thrown
as the result of a coding error within the method.

I do think an assert would be valid, if the compiler accepts that as
definite termination. Testing for improper program execution (bugs) is
exactly what assert is for.

But it's all a matter of personal style. I don't know the context.
 
G

Guest

Chris said:
This is a great idea in general (certainly better than just returning
incorrect values). Just curious, though -- two people now have
suggested throwing IllegalStateException. Yet there is no illegal
state. In fact, as near as I can tell, this method makes no use of
state at all. IllegalStateException implies that the method could be
called if the object were in a different state and it would succeed.

If there's no better exception and the condition is entirely impossible
to begin with, it seems far preferable to me to just throw
RuntimeException instead.

Just my two ¤cents:

public String getResponseType() {
// ...
switch(n%3) {
case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
default: throw new AssertionError();
}
}
 
M

Michiel Konstapel

If there's no better exception and the condition is entirely impossible
to begin with, it seems far preferable to me to just throw
RuntimeException instead.

Or, given the "can't happen" nature of the problem, InternalError.
Michiel
 
C

Chris Smith

Quebec said:
Just my two ?cents:

public String getResponseType() {
// ...
switch(n%3) {
case 0: return "EXACTMATCH";
case 1: return "RANGEMATCH";
case 2: return "ALTMATCH";
default: throw new AssertionError();
}
}

Yes, that makes sense, or to use more normal syntax:

default: assert false;

or

default: assert false : "Bad value for n % 3";

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tony Morris

Chris Smith said:
This is a great idea in general (certainly better than just returning
incorrect values). Just curious, though -- two people now have
suggested throwing IllegalStateException. Yet there is no illegal
state. In fact, as near as I can tell, this method makes no use of
state at all. IllegalStateException implies that the method could be
called if the object were in a different state and it would succeed.

The illegal state is that the developer foresaw that the default case would
never occur.
If it does occur, the application is in an illegal state.

"... Java application is not in an appropriate state for the requested
operation."

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top