?
Solved

Control in JAVA

Posted on 2002-07-24
11
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 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