Solved

Control in JAVA

Posted on 2002-07-24
11
239 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 60
mysql jsp example issue 32 74
ejb entity bean example issue 2 27
Running JavaFX on the Raspberry Pi 27 139
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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