Solved

Control in JAVA

Posted on 2002-07-24
11
230 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
Weekend adv creator 3 51
oracle 11g 23 106
arguments to jar 5 33
Facing this issue for maven proxy setting 2 19
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

789 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