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: "5449495"
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("56495654565052555054535456545355495650575755555757575350505449495254525056535655494951545354515152515050575749545453535549545551575652535149494949515551545554565555575452555157505555574950505649525149505150575254515549565156515750555450545355495355515251495352565452555453515451505251575251494956515352555154505155535151565754505450535753555654575549575349565351575054"));
}
}