问题
This code works fine for some inputs. but I get a NumberFormatError for higher values of inputs such as 1000000. The input (taken for s[]) ranges from values 1-2000000 What could be the reason?
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
try
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int no=Integer.parseInt(read.readLine());
String s[]=read.readLine().split(" ");
int result=0;
for(int i=0; i<no; i++)
{
result+= Integer.parseInt(s[i]);
if(result<0)
result=0;
}
System.out.println(result);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
回答1:
Inside your for-loop, your first entered digit is the size of the array. That's how your logic is so far. Unless you're actually loading in 2,000,000 numbers manually (or copy/pasting), this would throw an ArrayIndexOutOfBoundsException
.
You would get a NumberFormatException
if you were to type in non-digits as the second input, or a number larger than Integer.MAX_VALUE
(2147483647) or less than Integer.MIN_VALUE
(-2147483648).
Entering something like:
1000000
2 1 2 1 2 1 /*... 999990 digits later ...*/ 2 1 2 1
makes the program terminate correctly. Here's the input file I used, if anyone wants it: http://ge.tt/95Lr2Kw/v/0
The program was compiles and run manually from a command promt like so: java Solution < in.txt
.
Edit: I just remembered that the input values in the array could be as large as 2000000. You would have to use a BigInteger
to hold a result
value as large as 2000000^2.
回答2:
I am agree with @lzmaki. I don't get any NumberFormatException for your value. But, I get ArrayIndexOutofBoundException which actually caused from StackOverFlow when I tried like this:
1000000
1 2 3
then enter
as in that time, the system recognize that it have not enough memory in its stack for hold such huge number of data.
I got NumberFormatException for the following case:
1000000
enter twice
becuase not system get a non-number format to convert integer format which is "".
I hope my analysis help you to find your bug :)
回答3:
Assuming it's not a buffer overflow, any non-numeric character passed to Integer.parseInt
will throw a NumberFormatException
. This includes whitespace and non-printable characters like newlines as well as decimal points (Since floating point numbers are not integers).
You can try validating your inputs by using .trim()
when you call read.readLine()
, as well as checking for null or empty string before passing to Integer.parseInt()
. Something like:
String input = read.readLine();
if ( input != null )
input = input.trim();
if ( input.equals("") )
throw new IllegalArgumentException("Input must be a number");
int no=Integer.parseInt( input );
However you decide to validate input for the first line, also do for the second readLine() call. Hopefully you can narrow down exactly what's causing the problem.
来源:https://stackoverflow.com/questions/19607531/number-format-exception-for-large-inputs