Switch on String in tJava

Highlighted
One Star

Switch on String in tJava

Hello,
While using the tJava component we have an error when switching on a String:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted

Talend is however configured to use a Java 7 JRE:

How is that possible ?
Kind regards,
Ivan.
Highlighted
Community Manager

Re: Switch on String in tJava

Hi 
It is a compilation error, can you show us the Java code on tJava component?
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Highlighted
One Star

Re: Switch on String in tJava

Hi,
Here is the code.
I can't seem to add a screenshot though. It shows on my upload list and the editor but not in the final post.
switch (input_row.TYPE) {
case "A" :
context.table = "a";
break;
case "B" :
context.table = "b";
break;
case "C" :
context.table = "b";
break;
case "D" :
context.table = "c";
break;
case "E" :
context.table = "d";
break;
default :
throw new IllegalArgumentException("Unknown truncate action !");
}

And the error message :
Starting job a at 09:53 23/01/2015.
connecting to socket on port 3837
connected
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
at a.b.c.tFileInputDelimited_1Process(c.java:589)
at a.b.c.runJobInTOS(c.java:914)
at a.b.c.main(c.java:757)
Job a ended at 09:53 23/01/2015.


Kind regards,
Ivan.
Highlighted
Community Manager

Re: Switch on String in tJava

Hi 
It is a basic Java issue, you can't use string type in switch statement expression with your JDK version, see 
http://stackoverflow.com/questions/6231907/java-7-switch-statement-with-strings-not-working
http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog