1

I want to create a long[] Array with the Stringbuilder, but with this code i get "java.lang.NumberFormatException:"

protected long[] getpattern(int numbercount, int value) {
        StringBuilder longstringbuilder = new StringBuilder();

        while(numbercount > 0) {
            longstringbuilder.append("100, ");
            longstringbuilder.append(value + ", ");
            numbercount--;
        }
            longstringbuilder.append("100");

        String longstring = longstringbuilder.toString();

            long[] pattern = new long[] {Long.parseLong(longstring)};




        return pattern;

    }

How can i fix that? Thanks for your answers

2
  • Why do you think that your syntax is going to work? You are making an array with a single item in it, and the item comes from parsing a long and incorrect string, which causes the exception. Use a list instead of string builder. Commented Dec 17, 2017 at 17:23
  • 4
    Why would you use a type designed for manipulating text to build an array of numbers? What are you trying to accomplish here? Why not use long[] array = new long[numbercount]; and then populate it in a loop? (Or numbercount * 2 if you want a value of 100 between each value value.) Commented Dec 17, 2017 at 17:26

4 Answers 4

2

You know how many elements there will be in the array: two times numbercount, plus 1. So just build that array:

long[] values = new long[2*numbercount+1];
for (int a = 0; a < numbercount; ++a) {
  values[2*a+0] = 100;
  values[2*a+1] = value;
}
values[values.length-1] = 100;

There is no need to involve StringBuilder at all.

Sign up to request clarification or add additional context in comments.

Comments

0

Your 'longstringbuilder' reference contains nothing but a String with numbers, commas and spaces. You can parse a String which is %100 number, but in your case you have commas and spaces too.

There are several workarounds, for example you could split the String: String[] stringArray = longstringbuilder.split(", "); and then you can parse each item of your stringArray, since it will contain only numeric values.

Comments

0

Using a StringBuilder looks like the long way round. Using a Stream would be much simpler:

protected long[] getpattern(int numbercount, int value) {
    return IntStream.range(0, numbercount).mapToLong(i -> value).toArray();
}

3 Comments

Or if the OP isn't comfortable with that, just constructing the array and populating it with a for loop would be really, really simple.
I'm surprised there isn't a Winter Bash hat for getting a comment from @JonSkeet :-)
Also, one-liner FTW!
0

You need to get rid of the commas and spaces in your string. I am using trim in this example but you could probably just split on ", ".

protected long[] getpattern(int numbercount, int value) {
    StringBuilder longstringbuilder = new StringBuilder();

    while(numbercount > 0) {
        longstringbuilder.append("100, ");
        longstringbuilder.append(value + ", ");
        numbercount--;
    }
    longstringbuilder.append("100");

    String longstring = longstringbuilder.toString();
    String[] tokens = longstring.split(",");
    long[] pattern = new long[tokens.length];

    for (int i = 0; i < tokens.length; i++) {
        pattern[i] = Long.parseLong(tokens[i].trim());
    }

    return pattern;
}

1 Comment

This works fine, because there should be a 100 between every other value. Therefore twice as much numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.