Jump to content

  • Log In with Google      Sign In   
  • Create Account

Can someone explain what this means please


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 02 August 2012 - 03:30 PM

I got to this example in one of my java books. Typed it out, compiled it. It ran fine. But, I seen this warning or whatever it is. Is there anything wrong with this? How can I correct it? Is it something that is out of date?


From the book-
Posted Image
From Eclipse-

Posted Image

Also, is it better to learn using Netbeans or Eclipse or just something like notepad and cmd on windows?

Thanks for any help.

Sponsor:

#2 SamiHuutoniemi   Members   -  Reputation: 259

Like
0Likes
Like

Posted 02 August 2012 - 04:04 PM

Just use sc.close() to "shut down" the Scanner.

It is much easier to use an IDE like those you mentioned, as it gives you syntax highlighting, debugging, and easy compiling and running.

Edited by SamiHuutoniemi, 02 August 2012 - 04:05 PM.


#3 rip-off   Moderators   -  Reputation: 8210

Like
0Likes
Like

Posted 02 August 2012 - 04:41 PM

First of all, please copy and paste the code text on the forums, using [ code ] tags or the helpful edit buttons provided. This makes it easier for us because we can quickly copy your code into our own IDE and test it out.

To your question, some Java objects deal with external resources. For example, a file on your hard disk, or a connection to a network, or more esoteric resources still. These need to be explicitly managed and promptly closed when you are finished using them so that the operating system can do any maintenance and cleanup it needs to do. Most external resources can be cleaned up by the operating system when your program exits. However, it can only clean up what it knows about.

Sometimes data can be lost - for example, if you write some data to a file, the data will often end up in a buffer in memory (for performance reasons). It gets written out periodically as the buffer gets larger. Closing a resource will cause any remaining data in these buffers to be permanently written to the disk. If the file is not closed, when the program ends the operating system is not aware of the special nature of this buffer, so it doesn't know to put it in the file when it is cleaning up.

There are other consequences to not cleaning up resources. Long running programs are said to "leak" resources that they don't clean up. Some operation systems have limits on the number of open resources (files, network connections, etc) that a given program can use. If a program doesn't close all the resources, they'll eventually reach their limit and the operating system will refuse to open new resources for them. Thus the program stops functioning after some time.

This is particularly important in games, where the number of loops per second is very high. A resource leak such as not alerting the operating system / graphics driver that you are finished with a particular texture could result in the game slowing down between level loads and eventually failing to load a level.

To come back to the specifics, the Scanner class can be used to wrap all sorts of input devices, not just the interactive console. The interactive console is a relatively simple resource, there are few of the downsides mentioned above to reckon with. If you were wrapping a file or network connection, close()ing the scanner (which closes the "wrapped" device) would be much more important. However, it is good practise to close() it anyway.

You may not be aware of this, but it is possible to make your program read data from a file, and write it to a file. On windows, you could do this:
input.txt
10

console:
c:\path	o\project> java ComputeCircle < input.txt > output.txt
This tells the command interpreter to:

* Execute the java program
* Pass ComputeCircle as the parameter to the program
* Instead of the keyboard, read from "input.txt"
* Instead of the screen, write any output lines to "output.txt"
If you were to open output.txt subsequently, it should contain the same data that Eclipse's console output contains.

Now, all of a sudden, without changing the code, that close() call you omitted might be more important!

Also, is it better to learn using Netbeans or Eclipse or just something like notepad and cmd on windows?

I'd stick with the IDE until you are relatively familiar with the language. The exact IDE isn't all that important, provided it is a good one. I've heard good things about Netbeans and I've used Eclipse and it is good.

But it is valuable to understand the build process at a low level eventually. This is because someday Eclipse or Netbeans might have trouble that requires this understanding, or you might be in a situation where you want to compile code or run your program when you don't have an IDE installed and cannot do so. But I wouldn't worry too much for the time being.

#4 Jeffige   Members   -  Reputation: 113

Like
0Likes
Like

Posted 02 August 2012 - 05:11 PM

Thanks, SamiHuutoniemi for the info on sc.close()
Since I am a complete beginner, where exactly would I place sc.close() to close the scanner? On the next line after: Scanner cs=new Scanner(System.in);

First of all, please copy and paste the code text on the forums, using [ code ] tags or the helpful edit buttons provided. This makes it easier for us because we can quickly copy your code into our own IDE and test it out.


Understand. Thank you rip-off.

You may not be aware of this, but it is possible to make your program read data from a file, and write it to a file


Uh, no. Very unaware of that. I am a total beginner. I have been learning java for about... 7 hours or so. And 3 of those hours have been trying to get everything installed and running correctly. But hey, I ran my 1st two programs. A million more to go...

Edited by Jeffige, 02 August 2012 - 05:22 PM.


#5 rip-off   Moderators   -  Reputation: 8210

Like
0Likes
Like

Posted 02 August 2012 - 05:12 PM

You just need to close it before the end of your program. The last line would be a good place, as that way if you were to add more input statements later you might forget that you closed the scanner earlier.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS