1

I am trying to write code for a problem that involves a function, that takes input numbers as Strings, and returns the remainder of that input when divided by 7 as an int.

Although the code works for smaller numbers, it prompts a runtime error when handling large numbers as input, shown as follows ..

Runtime error   time: 0.04 memory: 711168 signal:-1

Exception in thread "main" java.lang.NumberFormatException: For input string: "5‌​449495"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.math.BigInteger.<init>(BigInteger.java:470)
    at java.math.BigInteger.<init>(BigInteger.java:597)
    at Ideone.remainderWith7(Main.java:13)
    at Ideone.main(Main.java:22)

My code is as follows ..

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
    int remainderWith7(String num) {
        // Your code here
        java.math.BigInteger bg = new java.math.BigInteger(num);
        //System.out.println(num);
        Integer n = bg.intValue();
        return (int) n % 7;
    }

    public static void main(String[] args) throws java.lang.Exception {
        // your code goes here
        Ideone id = new Ideone();
        System.out.println(id.remainderWith7("56495654565052555054535456545355495650575755555757575350505‌​44949525452505653565‌​54949515453545151525‌​15050575749545453535‌​54954555157565253514‌​94949495155515455545‌​65555575452555157505‌​55557495050564952514‌​95051505752545155495‌​65156515750555450545‌​35549535551525149535‌​25654525554535154515‌​05251575251494956515‌​35255515450515553515‌​15657545054505357535‌​55654575549575349565‌​351575054"));
    }
}
3
  • Good on you for editing and specifying the error. I only glanced and saw that you were using modulo after coercing the entire BigInteger down. I've figured out part of the issue now. Commented Nov 27, 2016 at 4:49
  • (I actually added the specific error after running it myself) Commented Nov 27, 2016 at 5:00
  • @qxz: Okay, thanks for that. Commented Nov 27, 2016 at 5:07

1 Answer 1

2

How'd you paste that string in? It looks like it contains a lot of zero-width spaces and zero-width non-joiners.

I say "looks like"; in actuality, you'd only ever be able to see those if you printed out the contents of the string's array, either with Arrays.toString and encapsulating that long string in a variable, or if you inspected it with a debugger.

Ultimately, that's what's leading you astray; Java is attempting to convert those Unicode characters to numbers, and since they're not numbers, they convert to -1. This is why your code breaks, and this is also why it's not immediately apparent as to why it breaks. There are more characters in that string than you're immediately led to believe.

The fix is to remove these characters from the string.

String num = ""; // enter your long number here; not repeating it for brevity's sake
num = num.replace("\u200C", "").replace("\u200B", "");

Now you can get back to other issues with the code, such as not using BigInteger.mod when you want to do a modulo (because trust me, using % ain't gonna give you the right answer with an integer as large as that).

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

Comments

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.