• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mysterious exception while handling file

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

this is the relevant code snippet:

...
inputBuffer = new BufferedReader(new FileReader(inputFile)); //lineA
testcases_T = Integer.parseInt(inputBuffer.readLine()); //lineB
...

When I'm in debug mode line A compiles fine and it even shows the correct value for the first String (1000) that i watch in the Tab "Expressions" in Eclipse (i added a watch for inputBuffer.readline()).
But when I try to compile lineB it throws this:

java.lang.NumberFormatException: For input string: "138 844"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at competition.gcj2013.round1A.BullsEye.main(BullsEye.java:30)

The input file is like this:

1000
138 844
....

so it kinda "ignores" or "jumps over" the first line???

I'm not getting this... help is really appreciated.

Thanks in advance!
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know when a NumberFormatException is thrown? What is "138 844"? It is a String alright, but is it in the format which can be successfully converted to an Integer type? I don't think 138 844 is a single number. I believe the whites pace is causing the issue. Check.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:Hi there,

this is the relevant code snippet:

...
inputBuffer = new BufferedReader(new FileReader(inputFile)); //lineA
testcases_T = Integer.parseInt(inputBuffer.readLine()); //lineB
...

When I'm in debug mode line A compiles fine and it even shows the correct value for the first String (1000) that i watch in the Tab "Expressions" in Eclipse (i added a watch for inputBuffer.readline()).
But when I try to compile lineB it throws this:

java.lang.NumberFormatException: For input string: "138 844"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at competition.gcj2013.round1A.BullsEye.main(BullsEye.java:30)

The input file is like this:

1000
138 844
....

so it kinda "ignores" or "jumps over" the first line???

I'm not getting this... help is really appreciated.

Thanks in advance!




Well. the exception is clearly the issue of doing a parse int of a string with a space in it. As for "jumping the first line", you only showed two lines of code, which is definitely out of context for us to see why the first line was skipped.

Henry
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(sorry for the format, but when submitting it ignores the tabs for indention)

this is the code from the start:

public static void main(String[] args) {
String dir = "C:\\Users\\bla\\bla\\bla\\bla\\bla\\bla\\bla\\"; //not the real path of course...
File inputFile = new File(dir + "A-small-practice.in");
File outputFile = new File(dir + "result.out");
BufferedReader inputBuffer;
BufferedWriter outputBufferedWriter;
int testcases_T;
try {
inputBuffer = new BufferedReader(new FileReader(inputFile));
testcases_T = Integer.parseInt(inputBuffer.readLine());


the funny thing is:

it doesn't throw this exception the FIRST time I run it... always the SECOND time...

this is already the second time this happens and I'm really clueless...
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we see the entire code Bora? Please. And use code tags to paste code.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, code tags... thanks.

ok, there you go:



And the inputfile is like this:

1000
138 844
21 197
82 866
19 295
181 731
77 477
163 ....

That's it.... hope this helps.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting NFE, although I am getting :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at test.BullsEye.main(BullsEye.java:31)


i.e at :


where it is trying to access the element at position 1 of the array of split numbers.

Also, please explain the use case in detail i.e. what is it that your code tries to accomplish? I can see that you are calculating area of circles. Also explain why you have written what you have. Does the input file contain only the inputs that you shared above or does it have more inputs? Why are you running the loop from 1 to 1000 after reading the first line?
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my solution to:

https://code.google.com/codejam/contest/2418487/dashboard

it is just a mathematical kind of exercise that I'm trying to implement in Java...

and you can get the input file if you go to "Solve A-small" and then to "Download A-small-practice.in".

This is a file which has all the input data that the program has to process...

Can you try it with this file? Let's see if it works with the inputfile... because if it works with the inputfile, then it is something with my environment that is wrong... and if it doesnt work.... then it is the code...
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is going into infinite loop. Check carefully. I have solved the sum on paper. I will try it myself tomorrow though.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another strange thing I encounter when I'm trying to debug is: it throws the exception only when I'm debugging... not when I simply run it without debugging...

does anyone know why this is like that (like exceptions being thrown only when going step-by-step through the program but not when running it)?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you run the code? And which exception comes when you debug it?
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I run it, it goes into infinite loop as you said... and when I try to debug, it throws the exception posted above

( it says:

java.lang.NumberFormatException: For input string: "138 844"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at competition.gcj2013.round1A.BullsEye.main(BullsEye.java:30)

whereas the 30 refers to the line testcases_T = Integer.parseInt(inputBuffer.readLine());

)

If I can get over this initial hurdle, I can handle the debugging of the program myself, but I don't know why it does not read the first item of the input file (which is '1000').
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:when I run it, it goes into infinite loop as you said... and when I try to debug, it throws the exception posted above

( it says:

java.lang.NumberFormatException: For input string: "138 844"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at competition.gcj2013.round1A.BullsEye.main(BullsEye.java:30)

whereas the 30 refers to the line testcases_T = Integer.parseInt(inputBuffer.readLine());

)

If I can get over this initial hurdle, I can handle the debugging of the program myself, but I don't know why it does not read the first item of the input file (which is '1000').



It is reading the first line of the input file Bora. When you say:



it has read that line. Hence, you observe that inside the loop it starts reading from the second line directly. Once a line has been read, it will not be re read. The pointer has moved to the next line after the first call to readLine() above. Are you with me?

Also, what is the logic that you have implemented to find the number of black circles that can be painted for a give set of input? Can you write that in plain English for me? I need to be sure first that your logic is correct.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I'm with you... it reads that first line (1000), but I don't know where that value is gone... remember my initial post:


inputBuffer = new BufferedReader(new FileReader(inputFile)); //lineA
testcases_T = Integer.parseInt(inputBuffer.readLine()); //lineB
...

When I'm in debug mode line A compiles fine and it even shows the correct value for the first String (1000) that i watch in the Tab "Expressions" in Eclipse (i added a watch for inputBuffer.readline()).

But when I try to compile lineB it throws this:

java.lang.NumberFormatException: For input string: "138 844"



So it gets the first line (1000), but when lineB is executed, it throws an exception, since it reads the next line already (138 844)... I'm wondering why this happens.

As far as the algorithm is concerned:

Since area A = Pi*radius *radius and you can use 1 ml for Pi cm2 you can exclude Pi from the calculation altogether.

Lets say radius =1:
You calculate the area of the outer black circle and extract from it the area of the inner white circle.
Since every circle has 1cm more radius it goes like this:
2*2*Pi - 1*1*Pi -> (4-1)*Pi = 3Pi
The next black circle would start at 4cm, so:
(4*4-3*3)*Pi = 7Pi, and so on...
you continue to do this until you reach the amount of ml you have for drawing which is in the first testcase 9. Thats why you can only draw 1 circle with 9ml.
The next testcase says 10ml, and with that you can draw exactly 2 circles (since you need 3ml for the first and 7 for the 2nd black circle)

I hope this was a little bit clear...
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I spent the last 2 hours in correcting your class. It works now. required a major overhaul though. First of all, lets start with the logic. You claim:

Since area A = Pi*radius *radius and you can use 1 ml for Pi cm2 you can exclude Pi from the calculation altogether.



What exactly do you mean by this? Can you put it mathematically?

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to work out the proportional difference in area between circles of successively larger radius? Where did you get the abbreviation ml from?
You know that (a + b)² = a² + 2ab + b², so the series of perfect squares 0‑1‑4‑9‑16‑25… increases by 2n + 1 where n is the square root of the current value. So, if you are using πr², and the radius increases by 1, the area will increase by π(r + 1)² ÷ πr², which is the same as (r² + 2r + 1) ÷ r². In that case, yes, π cancels out.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: Have you got the original problem about the number format and reading from the file sorted out correctly? You want to solve one problem at a time; you are liable to become confused if you try to do too many things simultaneously.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the ml info from the original problem statement I posted above (https://code.google.com/codejam/contest/2418487/dashboard ).
Actually I can handle the algorithm stuff on my own.
I just need help on the original exception thrown while reading the first line.

So yes, you are right. lets focus this thread on this single problem only and not on the algorithm.

Just help me to get over this initial problem of reading the first line and I will post the working code here. Promise
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still not getting any NFE while debugging the code that you pasted above. Although there are other flaws in your code which I can point to. It runs into infinite loop. That's it.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're not getting a NFE while debugging?

Please set a breakpoint to the first line after the try-clause (09. inputBuffer = ...)



then press F6 for 'Step Over' if you're using Eclipse so it goes to the next line (10. testcases_T = ...) and then press F6 again (if you're using Eclipse)....

when I'm debugging it throws the NFE there...

If you don't get the NFE when doing the steps described above... then there is some local problem with my environment that I have to solve first...

... and you're using the input file from the google code jam website don't you (A-small-practice.in)?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check my attachment snapshot. This is what I observe. No NumberFormatException.
Bora_Exception_Test.png
[Thumbnail for Bora_Exception_Test.png]
No NumberFormatException
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:Please set a breakpoint to the first line after the try-clause (09. inputBuffer = ...) ... then press F6 for 'Step Over' if you're using Eclipse so it goes to the next line (10. testcases_T = ...) and then press F6 again (if you're using Eclipse)....


Actually, don't, because it has absolutely nothing to do with your problem.

You have already been told what the problem is way back in the 2nd post of this thread - indeed the error message itself told you very clearly:
java.lang.NumberFormatException: For input string: "138 844"

Simply put: "138 844" is NOT a number. It's two numbers.

So forget all this debugging palaver and solve the problem. Now how do you think you might do that?

AFAICT you also still haven't explained why you're skipping that first line - which is a number.

Winston
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
AFAICG you also still haven't explained why you're skipping that first line - which is a number.



Thats why I opened this thread in the first place... because I don't get it myself why it is skipping it.


So Mansukhdeep is debugging it and it throws no exception.

Since when Mansukhdeep is debugging it, as we can see from the snapshot: IT READS THE FIRST LINE, "1000" !

I have the same code, the same input file and it is throwing an NFE since it skips the first line somehow (which contains the string "1000"), that's why the NFE is thrown.


Obviously it doesn't make sense trying to solve the problem, if you're getting different outcome when debugging, than you're supposed to get.

So we have to solve that problem first. Then we can proceed and solve the actual problem.


Mansukhdeep, can you tell me which environment you use (which IDE, is it Juno, Indigo? and then which JRE, which JDK, etc...)


I think I have to deinstall everything and then install again... some time ago I deinstalled Eclipse and installed NetBeans and played around with it and then deinstalled it again and installed Eclipse back again... since then my programs strangely don't behave the same way they used to...
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:

Winston Gutkowski wrote:
AFAICG you also still haven't explained why you're skipping that first line - which is a number.



Thats why I opened this thread in the first place... because I don't get it myself why it is skipping it.


So Mansukhdeep is debugging it and it throws no exception.

Since when Mansukhdeep is debugging it, as we can see from the snapshot: IT READS THE FIRST LINE, "1000" !

I have the same code, the same input file and it is throwing an NFE since it skips the first line somehow (which contains the string "1000"), that's why the NFE is thrown.


Obviously it doesn't make sense trying to solve the problem, if you're getting different outcome when debugging, than you're supposed to get.

So we have to solve that problem first. Then we can proceed and solve the actual problem.


Mansukhdeep, can you tell me which environment you use (which IDE, is it Juno, Indigo? and then which JRE, which JDK, etc...)


I think I have to uninstall everything and then install again...



I am using Eclipse Juno with JDK 1.6. You do not need to uninstall everything Bora. There must be some small little point where you might be going wrong. That is the challenge here, to isolate that mistake.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but we have the same input file, the same source code, everything is the same... but still it skips the first line in my case, in yours it doesn't... hmm... I have no clue why it does that.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:but we have the same input file, the same source code, everything is the same... but still it skips the first line in my case, in yours it doesn't... hmm... I have no clue why it does that.


Then plainly at least ONE of your assumptions is wrong. Java isn't out to get you, and if you do both in fact have the same setup then you WILL get the same result.

Based on what I see, you're also hamstringing yourself with all that superfluous code. Test what you need to test - and as far as I can see that is something like:ie, isolate the problem and get it working; then add more code.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS: The constructis redundant, since that's exactly what will happen anyway; which is why I removed that whole try..catch business.

Also: BigInteger already has a constant called ONE, so you don't need to define it yourself. TWO, on the other hand, you will have to define; but my advice would be to define it as:
private static final BigInteger TWO = BigInteger.valueOf(2L);

That way you can use it anywhere in the class you like.

Winston
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just installed Eclipse Juno and JDK 7 at my nephews laptop.... it debugs beautifully, as shown in the snapshot from Mansukhdeep...

So in this case it is a local problem with my environment, it has nothing to do with the sourcecode.

I will deinstall Eclipse and the JDK and install everything from scratch at my own machine... then I will see if I get the same result there... if so, then I will be able to solve that problem, since I will be able to debug properly then... and then I will print the result here since I promised it

Thanks for everyone who helped me out, especially for Mansukhdeep who invested quite some time and efforts... thanks alot
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and here is the working code, since I promised it:



it works on the small input file, but it takes way too long for the large input file... (there are 2 different files most of the time at the website of google).

Since the original problem has been solved I mark it as resolved.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bora Sabrioglu wrote:I just installed Eclipse Juno and JDK 7 at my nephews laptop.... it debugs beautifully, as shown in the snapshot from Mansukhdeep...

So in this case it is a local problem with my environment, it has nothing to do with the sourcecode.

I will deinstall Eclipse and the JDK and install everything from scratch at my own machine... then I will see if I get the same result there... if so, then I will be able to solve that problem, since I will be able to debug properly then... and then I will print the result here since I promised it

Thanks for everyone who helped me out, especially for Mansukhdeep who invested quite some time and efforts... thanks alot



You are always welcome.
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic