good way:
When you write a code like bellow:
import java.util.Scanner;
public class userio
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a line of text: ");
String line = input.nextLine();
System.out.println("you entered: "+line);
}
}
in marked line you always have warning when you write your code in eclipse.
Eclipse suggest you that you have resources leak. To eliminate this problem use case like bellow
import java.util.Scanner;
public class userio
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
try
{
System.out.print("Enter a line of text: ");
String line = input.nextLine();
System.out.println("you entered: "+line);
}
finally
{
input.close();
}
}
}
--------------------------
Brak komentarzy:
Prześlij komentarz