Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Closing a Scanner object

807600Sep 15 2007 — edited Sep 17 2007
I have a question, hopefully some one will be able to answer,

as you can see in the code example below, if I were to uncomment input.close once myName2 gets to first=input.nextLine(); it throws an exception.
My question is: Isn't input local to getName()? Iwould think that when getName() is run for the second + time(s) it would create a new object, but instead seems to close it for the rest of the input. I've tried to think of why this would happen but I admit, I'm stumped.
Any help as to why this happens would be appreciated,
Alan

PrintName:
import java.util.Scanner;
 
public class PrintName
{
private static Name getName()
{
String first, last, middle;
Scanner input= new Scanner(System.in);
System.out.println("enter first name:");
first=input.nextLine(); //on the second go around, we get an exception here
System.out.println("enter last name:");
last=input.nextLine();
System.out.println("enter middle name:");
middle=input.nextLine();
// input.close(); // Closing the Scanner object closes it for the rest of the program?
return new Name(first, last, middle);
}
 
public static void main(String[] args)
{
Name myName;
myName=getName();
System.out.println(" first-last format:"+ myName.getFirstName()+" "+ myName.getLastName());
System.out.println(" first-last-middle format:"+ myName.getFirstName()+" "+ myName.getLastName()+", "+myName.getMiddleName());
 
Name myName2;
myName2=getName();
System.out.println(" first-last format:"+ myName2.getFirstName()+" "+ myName2.getLastName());
System.out.println(" first-last-middle format:"+ myName2.getFirstName()+" "+ myName2.getLastName()+", "+myName2.getMiddleName());
 
System.exit(0);          
}
}
{code}


Name:
{code:java}
public class Name
{
    private String first,last,middle;
    
    public Name()
    {
        first="";
        last="";
        middle="";        
    }
    
    public Name(String fName, String lName,String mName)
    {
        first=fName;
        last=lName;
        middle=mName;
    }

    public String getFirstName()
    {
        return first;   
    }
    
    public String getLastName()
    {
        return last;   
    }
    
    public String getMiddleName()
    {
        return middle;   
    }
}
{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 15 2007
Added on Sep 15 2007
8 comments
651 views