#tf is this error

1 messages · Page 1 of 1 (latest)

brisk pendant
#

my code

public int[] plusOne(int[] digits) {
        
        String num = "";

        for(int i = 0; i < digits.length; i++) {
            num += digits[i] + "";
        }

        int nums = Integer.valueOf(num);
        nums++;

        num = String.valueOf(nums);
        int[] array = new int[num.length()];

        

        for(int i = 0; i < digits.length; i++) {
            if(digits.length < num.length()) {
                array[i] = Integer.valueOf((String.valueOf(num.charAt(i))));
            }
            else {
                digits[i] = Integer.valueOf((String.valueOf(num.charAt(i))));
            }
            
        }

        if(digits.length < num.length()) {
            return array;
        }

        return digits;
    }
oblique graniteBOT
# brisk pendant my code ```java public int[] plusOne(int[] digits) { String num...

Detected code, here are some useful tools:

Formatted code
public int [] plusOne(int [] digits) {
  String num = "";
  for (int i = 0; i < digits.length; i++) {
    num += digits[i]  + "";
  }
  int nums = Integer.valueOf(num);
  nums++;
  num = String.valueOf(nums);
  int [] array = new int [num.length() ] ;
  for (int i = 0; i < digits.length; i++) {
    if (digits.length < num.length()) {
      array[i]  = Integer.valueOf((String.valueOf(num.charAt(i))));
    }
    else {
      digits[i]  = Integer.valueOf((String.valueOf(num.charAt(i))));
    }
  }
  if (digits.length < num.length()) {
    return array;
  }
  return digits;
}
#

<@&987246399047479336> please have a look, thanks.

oblique graniteBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

brisk pendant
#

it seems like i cant do this int nums = Integer.valueOf(num);

#

but i dont get why

proud dock
#

Because 9876543210 is bigger than what int can take which is 2147483647(2^31 - 1) ~ -2147483648(-2^31).

brisk pendant
#

in what data type can I save it then

proud dock
#

long

brisk pendant
#

long aint working

proud dock
#

what do you mean long aint work?

brisk pendant
#

i tried with long

#

its still the same error

proud dock
#

what's the code you edited when you said you tried with long?

brisk pendant
#
long nums = Integer.valueOf(num); ```
#

i did that

proud dock
#

you should try sth like this

long nums = Long.valueOf(num);
brisk pendant
#

guess im dumb as shiP

#

thx

#

dude

#

@proud dock

#

is there something bigger than long

proud dock
#

BigInteger or BigDecimal

brisk pendant
#

how can I implement this for that case

knotty narwhal
brisk pendant
#

but

knotty narwhal
#

long is pretty big

brisk pendant
#

i need a bigger one

knotty narwhal
#

like what is the size of your numbers ?

brisk pendant
#

for that string maybe

knotty narwhal
#

ah

#

indeed

#

use BigInteger

brisk pendant
#

i hate biginterger

knotty narwhal
#

!doc BigInteger#valueOf

brisk pendant
#
long nums = Integer.valueOf(num); ```
#

how to implement this

#

bigint

brisk pendant
#

is this java

feral willowBOT
#
public class BigInteger
  extends Number
  implements Comparable<BigInteger>

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification . For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder.

<br />

knotty narwhal
#

there should be a constructor that accept a String

#

so you should be able to do new BigInteger(string)

#

also, don't use Integer.valueOf, use Integer.parseInt instead, same for Long

brisk pendant
#

nice

#

i did that

#

but

#

if I wanna add up one to biginteger

#

how to do that

#

that wont work

knotty narwhal
#

look at the method BigInteger has

brisk pendant
#

ok

#

add

#

isnt that rightß

#

?

knotty narwhal
#

now you need to store the new biginteger you just got in the variable

brisk pendant
#

thank you

#

it works

knotty narwhal
#

👍