0

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?

1
  • 3
    You should not be using StringTokenizer. Its Javadoc says, "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code." Also, you should check for null when reading a line from a BufferedReader as that is how it signals end of file. Commented Feb 1, 2020 at 1:17

3 Answers 3

2

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]);
Sign up to request clarification or add additional context in comments.

2 Comments

I replaced the StringTokenizer with String.split() method however it doesn't change anything. The error Exception in thread "main" java.lang.NumberFormatException: For input string: "84 995058150" However, in my text, there's only a number like: 108347484 995058150 So the numbers get cut down, but I do not know why?
It is not possible for line.split(" ") to return an array where any part has a space; is it possible that the actual text contains a character that looks like a space but is not one, e.g. a non-breaking space character? I'm not sure how split could separate the 84 from the rest of that number, maybe it is to do with pasting the input?
1

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

enter image description here

Comments

-1

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.

2 Comments

The .trim() method returns the same string, so your code throws exactly the same exception.
@kaya3 Yes, It's my mistake. It's only work with the value (" 84995058150" or "99505815084 ").

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.