AR2R
AR2R

Reputation: 176

Java console input length limit

I have an interview task where I was given a text file with file.in format, the task said that my program should be using standard data input and output (I assume console). The input file goes something like this:

249089
439506849 989399339
773359725 989399094
33290819 989399230
771114928 989399164
823133180 989399164
615096154 989399094
340750872 989399164
41881535 989399230
637599407 989399339
510268939 989399506
46219619 989399544
221332387 989399659
236968778 989399824
902942034 989399945
936095694 989400101
**end to the line 249090**

The first number is the number of objects The second is two numbers, but for the purpose of the task I only use the second one

For the purpose of parsing the numbers I use for loop and code below:

try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)))
String line = bufferedReader.readLine();
            System.out.println(line);
            StringTokenizer stringTokenizer = new StringTokenizer(line, " ");//
            stringTokenizer.nextToken();
            int height = Integer.parseInt(stringTokenizer.nextToken());

I use IntelliJ build in console and when I paste into console i get like a couple thousands results in starting from the end, so the first number is wrong, and when i run my program i get Runtime Error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "84 995058150"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:658)
    at java.base/java.lang.Integer.parseInt(Integer.java:776)
    at pl.artur.Main.getNumberOfBuildings(Main.java:23)
    at pl.artur.Main.main(Main.java:14)

Is there a way to get around it using standard input?

Upvotes: 0

Views: 616

Answers (3)

AR2R
AR2R

Reputation: 176

Ok, I resolved the issue. The solution was to set the bigger buffer size for the console in IntelliJ settings:

enter image description here

Upvotes: 1

mixin27
mixin27

Reputation: 159

I think you cannot convert or parse string including empty spaces between them to Integer.

// This string cannot be parsed to Integer directly.
// Because an empty space is included between `84` and `995058150`.
String s = "84 995058150";

If you want to parse this, you should use trim() method. example:

int intValue= Integer.parseInt(s.trim());

I hope this answer will be helpful for you.

Upvotes: -1

kaya3
kaya3

Reputation: 51043

This has nothing to do with where the input comes from; as the stack trace shows, the exception is thrown by the Integer.parseInt method on the string "84 995058150". This string clearly does not represent a (singular) integer. If the StringTokenizer.nextToken method returns this string, then it is StringTokenizer that's the problem. As David Conrad notes in the comments, the documentation says:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The String.split method will split line into the two parts, so you can then call Integer.parseInt on the part you want:

String line = bufferedReader.readLine();
String[] parts = line.split(" ");
int height = Integer.parseInt(parts[1]);

Upvotes: 1

Related Questions