• Post Reply Bookmark Topic Watch Topic
  • New Topic

New and Self Teaching question.  RSS feed

 
Jason Harrod
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I started learning Java last week and have very little prior programming knowledge (one course in highschool about C++ 10+years ago). I found this problem online Problem and wrote the following to try and complete the question in Java rather than Python. When I compile what I wrote the value (line 23) it is not entering the value into the Float value gal. Can someone point out what I'm sure is obviously wrong.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing your calculations before you get the input value. you should get the input value before you do the calculations.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Jason Harrod
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank your for the quick response. After making the adjustment everything is working as intended, but I have a questions about line 9. I have a message of "resource leak"amount" never closed" when I'm viewing it in Eclipse. What does this mean and what can that potentially do with a program?

 
Zulfiquiar Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason

you are getting this resource leak never closed message because you have obtained a resource that is System.in but you have not closed that resource once you are done

See when you write


you are creating a handle for the System.in resource( that by default is keyboard) to read data from it.
And keeping the handle even after your work is done is considered bad.
So after your work of reading the data is complete you should in your code close the connection to the resource you are using.
Hence you have to write the below line of code and your work will be done



this close() closes the connection to the resource and resource becomes free to be used by some other thread

Make it a thumb rule to always close connection of resources once you have used them
 
Zulfiquiar Khan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resource leak can cause serious problem to programs running on the system.
Because your program even after using the resource have not released it or closed the connection to the resource .
The resource would not be available for other programs and it may cause starvation of programs that are waiting for that particular resource.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again.

Never mind how bad a resource leak is, closing System.in (and System.out and System.err) is worse. Try it and see what happens
Never never never never close a Reader Writer Scanner Formatter etc pointing at any of those streams. There are two solutions to your problem:-
  • 1: Learn to ignore that particular warning for those particular Streams.
  • 2: Don't use Eclipse or other IDEs until you are more experienced.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!