-
December 21st, 2013, 08:03 PM
#1
Close Scanner
Hello, I've been going through a tutorial and have copied some code from it, It give's me an error prompt (well not error, the program still runs, more of a suggestion I guess...) telling me to close the scanner, yet in the code that I've copied there's nothing on the left of his screen... Im a bit confused why this is.
here's the code in my Eclipse ;
here's the code in his without the little error thing on the left
My program does the expected (asks for number then ends when the right one is given) but it has that little prompt on it.
If anyone could shed some light that'd be great.
Cheers
-
December 22nd, 2013, 12:02 PM
#2
Re: Close Scanner
Could you post here the error messages and code you are asking about? Not images because text can't be copied from images for including in answers.
Norm
-
December 22nd, 2013, 12:04 PM
#3
Re: Close Scanner
Cheers Norm - should be able to get at my computer later and I'll post it up.
-
December 22nd, 2013, 12:43 PM
#4
Re: Close Scanner
At eclipse, it probably asks you to close the scanner after you are done listening the console.
It is always good practise to close inputstream once you are done getting input when reading file, reading socket or reading a console. It costs extra threads, blocks further processing and causing hard to find bugs in the application not to close streams.
Eclipse's warning is probably about this.
I am not sure about the syntax but scanner.close(); is what you should do once you get what you are looking for.
-
December 24th, 2013, 06:52 PM
#5
Re: Close Scanner
Sorry I forgot about this post - here is the code that I wrote though with the error on line 7 :
Code:
import java.util.Scanner;
public class ScannerClass {
public static void main(String[] args) {
//Create scanner object
Scanner input = new Scanner (System.in);
//Output the prompt
System.out.println("Enter some text:");
//Wait for the user to enter something
short Character = input.nextShort();
//Tell them what they say
System.out.println("You entered : " + Character);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|