1

I have a string array which i need to parse it to long variable.

The string array contains values as ["9", "8"] etc. I want to convert this to "(9,8)" (I am using it in the "IN" clause of an SQL statement).

What is a good way to accomplish this?

Here is my code:

String[] statusIdArray;
long statsId = Long.parseLong("(");
statsId = Long.parseLong(statusIdArray[0]);
  for(int i=1; i < len; i++) {
    statsId = Long.parseLong(statsId + ",") statsId=Long.parseLong(statsId + statusIdArray[i]);
    statsId = Long.parseLong(statsId + ")");
  }

But I get a NumberFormatException at input string "(9"

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
prasanna
  • 51
  • 2
  • 9
  • I edited this off of some comments below. One thing to check is, do you start with `long[]` or `string[]`? Or, perhaps I completely messed it up. In which case, revert it :) Specifying which SQL statement (e.g. by providing some code) is being used will help clear up the problem/task. –  Jan 12 '11 at 07:00
  • I think you should rephrase your question without using the word "long" because it's confusing people. I suspect the answer is more along the lines of the Java version of String.Join – Joel Mueller Jan 12 '11 at 07:08
  • Please update your question so it is coherent. I have put in some code that was in a comment. –  Jan 13 '11 at 03:45

4 Answers4

1

Assuming that the question is: I have an array of Strings which I want to convert to SQL notation so I may use these in a SQL query:

import java.util.Arrays; // may need to add this import somewhere among your import statements

/**
 * Convert Java String[] to SQL array with varargs syntax.
 * Example method invocations:<blockquote><pre>
 * String sql1 = toSQLArray("1","2","3"); // should return "(1,2,3)"
 * String sql2 = toSQLArray(new String[] { "0", "5" }); // should return "(0,5)"
 * </pre></blockquote>
 * @param array string array.
 * @return String created by concatenating each array element using "," as separator and 
 * adding "(" and ")" as delimiters for the result. 
 */
public String toSQLArray(String... array) {
  StringBuilder sb= new StringBuilder(Arrays.toString(array));
  sb.replace(0,1, "(");
  int l = sb.length();
  sb.replace(l-1, l, ")");
  return sb.toString();
}
user268396
  • 11,576
  • 2
  • 31
  • 26
  • guys the code looks like this String[] statusIdArray ; long statsId = Long.parseLong("("); statsId = Long.parseLong(statusIdArray[0]); for(int i=1; i < len; i++) { statsId = Long.parseLong(statsId + ","); statsId=Long.parseLong(statsId + statusIdArray[i]); statsId = Long.parseLong(statsId + ")"); } In the statusIdArray[] i have elements as 9 8 7.i am getting number format exception(exception at input string "(9" – prasanna Jan 12 '11 at 08:26
0

What do you men by "parse this string array to long as (9,8)?"

Do you have an array of Strings? Or just one String? Do you want each digit (separated by a comma) to be entered into a Long array? Or would you want (9,8) to become a Long "98"?

Vinay
  • 61
  • 7
0
// digits is your array
String result = "IN (";
for (int i = 0; i < digits.length; i++) {
    result += digits[i] + (i != digits.lenght - 1) ? ", " : "";
}

result += ")";

I think I got it right now.

You have to be carefull with the contents of the array, you could be a victim of an SQL Injection attack.

Marcelo
  • 2,232
  • 3
  • 22
  • 31
  • i have a string array which consists of digits.say example (9 8) i have to pass this string array as a long variable to the "IN" clause of sql.it should be like (9,8) when the long is passed – prasanna Jan 12 '11 at 06:55
  • Probably if you plan to concatenate string, the "String" type is not the best choice cause it's immutable. Use "StringBuilder" instead. – Alexandr Jan 12 '11 at 08:23
  • @Alexandr: I was in the understanding that newer versions of the Java compilers optimize it automatically to avoid the performance degradation of concatenating strings, i.e. the concatenation gets auto-translated into StringBuilder operations (I don't even remember why, maybe I'm wrong.) – Marcelo Jan 12 '11 at 15:42
  • I haven't heard about it. Probably you are right. I'll try to investigate it:) – Alexandr Jan 13 '11 at 00:37
  • I did a quick search. It looks like I'm wrong, the kind of optimizations I was missunderstanding refer to situations like c = "a"+"b"; in which case the compiler would optimize as c = "ab"; It still sounds to me like it's not only posible what I was suggesting (misunderstanding,) but it will be a good thing that just make sense. To me it looks like a very straighforward translation. Anyway, everybody seems to recommend using StringBuilder when you append strings in loops. I was ignoring it thinking about a compiler optimization that looks like its not there. – Marcelo Jan 13 '11 at 04:22
0

Question is a bit unclear but maybe

long[] result = new long[ strings.length ];
for (int i=0; i<result.length; i++){
    result[i] = Long.parseLong( strings[i] );
}

i have a string array which consists of digits.say example (9 8) i have to pass this string array as a long variable to the "IN" clause of sql.it should be like (9,8) when the long is passed

In this case use Jakarta Commons StringUtils (or something else with a join method)

sql.append("IN (");
sql.append(StringUtils.join(strings, ','));
sql.append(")");

I am usually also totally against direct interpolation of variables into SQL (you should use bind variables), but that is a bit tricky with a variable-length IN list. Just make sure you don't get weird data in that String array.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • This question: http://stackoverflow.com/questions/337704/parameterizing-a-sql-in-clause has some tricky solutions to the problem of parameterizing variable-length lists in IN clauses. (It's a famous SO question.) – Marcelo Jan 12 '11 at 07:08