Java snippet to create numeric range or Collection of strings

Hi,

I have spent the Sunday, so far, trying to create a numeric range with the help of Java Snippet. I know there is “Create Numeric Range” node made in 2023, that works but I hesitate - what if it somehow becomes obsolete?!

I have tested like 20 different suggested methods by using K-AI and CoPilot, but even if I state it is for KNIME AP 5.5.0 (it didnt work yesterday with a previous version released a few months ago) - however the directions are misleading and ends in red wavy-underlining.

In the latest code provided by K-AI, underneath there are some strange comments in K-AI:

In the “Output” section, declare a
variable named yourStringOutput
of type Object or Collection.
Use the same name in your code
to assign the list

The problem lies in that there is no option for Object or Collection in the dropdown list. So I just choose List and checked array.
Test Java Snippet.knwf (3.5 KB)

I take you mean the shared component here:

I wouldnt worry too much about it disappearing…

I’m afraid the WF you provided was empty so I’ve created a workflow that creates a range with some KNIME nodes…

Overview:

Workflow:

create range.knwf (78.3 KB)

I know it doesn’t use Java Snippet, but this problem can be solved w/o coding easily so might be an alternative :slight_smile:

Thanks MartinDDDD,

Yes there may be many ways to do things.

I’ll take your WF and check it out.

However, I am still curious about how bad the instructions were with K-AI and all. For future reference I’d like to have someone commenting my WF. If it was empty I’ll drop it back in another attempt to get a clearer understanding of how Java Snippet node works. I know that it was much easier the first time I used it, maybe 8 years ago!?

Again, thanks!

Test Java Snippetv2.knwf (119.3 KB)

Hi @Batjesen , adjust your java snippet as follows and it should work:

ArrayList <String>years=new ArrayList<String>();

for (int i = c_start_year; i <= c_end_year; i++) {
	
	years.add(String.valueOf(i));
}

out_yearStringsOutput = years.toArray(new String[0]);

Note that in your Output section, the output had become “Array of Reader (string)”. I’ve never fully understood that one, but you can double-click on it and then change it in the dropdown:

In your code, you actually want your output to be a String [] but in your code you were starting with an int[] then turning this into a List but you’d have to then convert that back to a String[].

In the code above, I could have started with a String[] and not used any collection object at all, but this then requires you to create an array of the required size, and populate it using subscripts like you were doing with the int[] array.

int numberOfYears = c_end_year - c_start_year + 1;
String[] years=new String[numberOfYears];


for (int i = 0; i < numberOfYears; i++) {
	
	years[i] = String.valueOf(c_start_year+i);
}


out_yearStringsOutput = years;

However, I find it easier (less prone to error in calculation :wink: ) to use an ArrayList of String initially so I don’t have to bother myself with defining the size upfront, and then convert that to the required String[] at the end.

Conversion from ArrayList to String array uses the following boilerplate code:
arrayListObject.toArray(new String[0]);
which tells it to create it as an array of Strings.

1 Like

Thank you takbb,

That is an elegant way of doing it - all in one iteration!

Well I do not know my ways around in Java, I do not do much programming at all nowadays, but was fluent in C++ and C#. And also the suggestions from K-AI and CoPilot took the longer route…

It is a bit distressing to not understand what’s in the output section and found no easy documentation to understand it. My interest of having this Java Snippet working and understand it, is for eventual future need of more direct approach than lots of nodes to work around a gap in the nodes.

Again, thank you! Problem solved!

Regards,

1 Like

You’re welcome @Batjesen . I’m getting a bit rusty on my java these days although used to be a java developer, so I am often heading to chatGPT for “reminders” (e.g. how to convert a ArrayList into a String[] which imho is more convoluted than it should be!)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.