Solved

Control in JAVA

Posted on 2002-07-24
11
215 Views
Last Modified: 2010-03-31
Friends, need your help.

I have a problem with this control code. Have a look,
                                                     
 <%                                                  
   if (rs10.next()) {                                
   String custcde = rs10.getString("TA1013");        
   out.println(custcde); // this will print out "1"                              
                                                     
   switch (custcde) {                                
    case 1 :                                          
       out.println("good");                          
       break;                                        
    }        // but this doens't work...

  if (custcde == 1) {
      println("hello");
}
// this one prompt me error, saying that "Incompatible type for java.lang.String. Can't convert  java.lang.String to int.

My TA1013 is stored as CHAR and I declare the custcde as String. But if I change to

int custcde - rs10.getString("TA1013");

it'll prompt me that wrong datatype.....


Any idea how to control??



                                   
                                                     
0
Comment
Question by:foongkim1
11 Comments
 
LVL 3

Expert Comment

by:rjackman
ID: 7176276
hi
u can try this out
retrieve it as a string the way u are doing, then convert string to int like
int i = new Integer(custcde).intValue();
then use this int variable in switch case
cheers
RJ
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 7176349
Hi foongkim1,

    The first thing, you can not use string parameter in "switch" case, it has to be a int. So as RJ said, if it is possible to convert the string that you have to int, you can do as RJ said. Otherwise you have to go for the "if" conditional statement.

thank u
siva
0
 

Author Comment

by:foongkim1
ID: 7176441
-- intValue

public int intValue()

Returns the value of this Integer as an int.

Returns:
the int value represented by this object.
Overrides:
intValue in class Number


http://java.sun.com/products/jdk/1.1/docs/api/java.lang.Integer.html#intValue()

This web-sites said it'll return the value of this Integer as an int. But my main value is in Char/String. How can pass the value in this API??

But it's done lah. I mean my problem. Just feel doubt of this documentation from Sun. Mayube can share with me how to read this API documentation.
0
 

Author Comment

by:foongkim1
ID: 7176481
OK. My code solve. But when I need to display that time they prompt me problem.


<%
....
....
 int i = new Integer(custcde).intValue();                                          
 switch (i){                                
   case 1:                                  
      custclass = "DIRECT CUSTOMER";        
      break;                                
                                           
   case 2:                                  
      custclass = "DEALERS";                
      break;                                
                                           
 %>

<table>
<td> CUstomer Class </td>
<td> <%= custclass %> </td>
</table>


And the exception is "Variable custclass may not have been initialized".
But if I print the "custclass" right after the case,

case 2:                                  
      custclass = "DEALERS";                
      out.println(custclass);

It's work in this way.


Please help........

                                           
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7176544
You must initialise variables as follows:

String custclass= null;
int i = new Integer(custcde).intValue();                                          
switch (i){                                
......
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:flumpman
ID: 7176612
In this case you would might want to initialize the variable as

String custclass = "";

So that if it is not found in the switch statement it won't output the word "null" into the table.

But as CEHJ is alluding to, the important thing is that you initialize the variable.

It is also worth noting that if you add a "default" case to the switch statement and set the custclass String it will compile as the variable is guaranteed to be set before it is used.

e.g.
String custclass;

int i = new Integer(custcde).intValue();
switch (i){                                
case 1:                                  
    custclass = "DIRECT CUSTOMER";        
    break;                                
                                           
case 2:                                  
    custclass = "DEALERS";                
    break;

default:
    // will catch any other values of i
    custclass = "";
    break;
}

And finally...

it's better to use

int i = Integer.parseInt(custcde);

instead of

int i = new Integer(custcde).intValue();

as it avoids creation of an Integer object that isn't used.

--
flumpman
0
 

Author Comment

by:foongkim1
ID: 7176616
TQ CEHJ,

but wondering why must I initialise it like this?
In what conditions should I do this?

Normally I just decalre like that and it;s ok already
0
 

Author Comment

by:foongkim1
ID: 7176621
TQ CEHJ,

but wondering why must I initialise it like this?
In what conditions should I do this?

Normally I just decalre like that and it;s ok already
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7176636
>>but wondering why must I initialise it like this?

This is because this will be resolved as a local variable in a function. Local variables must be initialised beforehand as they are not automatically initialised by the JVM, as opposed to class or instance variables.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 7176655
>>
It is also worth noting that if you add a "default" case to the switch statement and set the custclass String it will compile as the variable is guaranteed to be set before it is used.
>>

This is true but in:

>>
So that if it is not found in the switch statement it won't output the word "null" into the table.
>>

of course, the variable custclass is being *set* in the switch statement, no sought.

In this case, it may well be better to first set it to null and then check for null after the switch statement. It could be misleading to have a valid String that does not contain any data in terms of the program logic. Of course if there *is* a need to set another visible value, such as "UNKNOWN", this could be set in default label.

0
 

Author Comment

by:foongkim1
ID: 7184769
CEHJ thanks for your professional guides.

TQ!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now