Solved

Control in JAVA

Posted on 2002-07-24
11
202 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
Comment Utility
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
Comment Utility
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
Comment Utility
-- 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
Comment Utility
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
Comment Utility
You must initialise variables as follows:

String custclass= null;
int i = new Integer(custcde).intValue();                                          
switch (i){                                
......
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:flumpman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
>>
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
Comment Utility
CEHJ thanks for your professional guides.

TQ!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
json format text only 4 64
advertisement module in core php 4 78
thymeleaf natural templating vs JSP 2 22
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 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

6 Experts available now in Live!

Get 1:1 Help Now