Results 1 to 13 of 13
  1. #1
    ezee is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Please describe this code

    Java Code:
    import java.util.Scanner;
    
    class InputExp {
    
       public static void main(String[] args) {
    
           String name;
           int age;
           Scanner in = new Scanner(System.in);
    
           // Reads a single line from the console
           // and stores into name variable
           name = in.nextLine();
    
           // Reads a integer from the console
           // and stores into age variable
           age=in.nextInt();
           in.close();
    
           // Prints name and age to the console
           System.out.println("Name :"+name);
           System.out.println("Age :"+age);
    
        }
    }
    specifically about "import java.util.Scanner" then "Scanner(System.in)" and "in.close"
    Thank You...:)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Sun's Tutorial has sections on the import statement and Scanner. In fact, I'd reccommend the whole "Learning the Java Language" and "Essential Classes" sections.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It reads a name and age from user input, and prints them.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Now crossposted on Sun forums

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    Now crossposted on Sun forums
    Not a good practice. :mad:

    And also that code is commented. So why our thread starter can't read it and try to understand. If he/she stuck on something better to ask specific questions here.

  6. #6
    ezee is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    Sorry..........actually my question is about "Scanner( System.in )" that what does it mean? And in.close !!!

    I am learning Java from "The Complete Reference". In it's examples, values to variable is given in code. I am keenly interested in learning java. So, I searched for it on google and got my solution, but couldn't understand some statements.

    So, i put my doubt here. please take me out from it. I am a computer science student, so i go to college daily. Therefore, I can't be online anytime. So understanding my compulsion, please guide me...
    Last edited by ezee; 03-16-2009 at 04:24 PM.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Scanner class allows simple console input read and the file input read. Even if you have a UI for your application, in background you can use he scanner to read files and so on.

    System class allows to deal with standard input, standard output and standard error outputs in Java. Three fields are use respectively, System.out, System.in and System.err

  8. #8
    ezee is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Question

    1) Why we r initializing Scanner object by System.in? What will happen by this statement?

    2)In "import java.util.Scanner", What is java,util and Scanner? How we will read/pronounce this statement?

    3) What will happen by the statement "in.close"?
    Last edited by ezee; 03-18-2009 at 10:48 AM.

  9. #9
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Hello :),

    1) Why we r initializing Scanner object by System.in? What will happen by this statement?
    "System.in" as I understand it tells the computer that the input for the variable is coming from the keyboard.
    "System" -> Computer
    "in" -> Input

    In java the basic Building block are classes. I did'nt understand the concept of classes until we started object Oriented Programming in Java, so you could expect the same.

    Java is all made up of classes...

    2)In "import java.util.Scanner", What is java,util and Scanner? How we will read/pronounce this statement?
    The import statement loads the classes that you want to use in your program. like you used...

    Java Code:
    import java.util.Scanner;
    is like saying "I want a Scanner from Util in Java". An Analogy to help you understand this is to picture a kitchen, and you are the cheif so you want to make a nice egg for yourself, or someone. Some of the tools you need should be already on the table like Knife, and the Frying pan.This could be like the java.lang which contains System.in. However, for the egg you need to check the Fridge, then pick up the egg.

    Likewise in java, you need to tell the computer what ingredients you need to use and where they are..

    so if it's in the kitchen, it would something like ...

    Java Code:
    import Kitchen.fridge.egg;
    or in your program

    Java Code:
     import java.util.Scanner;
    for the last question you asked, I really dont know because I have never used it... I just use the scanner for nextInt(), and nextLine() .

    Though, I am GUESSING it is to close the input stream.
    Last edited by MuslimCoder; 03-18-2009 at 02:17 PM.

  10. #10
    ezee is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Question

    1) Is "in" a object? [REF "Scanner(System.in)]" here i m understanding that "in" is an object of class System and we r passing it, am i right?

    2) B'coz this code is making a "in" object[Scanner in = new Scanner(-do-)]. So, i assume Scanner as a class (correct, if wrong) then what is "util" and then what is "java"?
    They are folder or anything else? (please define)
    if they are folder, then how compiler get the path for every same like this?

    Thanks Muslimcoder....
    Last edited by ezee; 03-18-2009 at 06:09 PM.

  11. #11
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    Ah, I see why you are getting confused.

    Java Code:
    Scanner in = new Scanner(System.in);
    They have used "in" in two places, but they are there for different purposed. Scanner in = new Scanner is creating a new object called in, but (System.in) is like MuslimCoder described. That line is basically saying "Create a new object of type Scanner called in and get its input from System.in (Keyboard)".

    You could change the object name if it makes it easier to understand.
    Java Code:
    import java.util.Scanner;
    
    class InputExp {
    
       public static void main(String[] args) {
    
           String name;
           int age;
           Scanner input = new Scanner(System.in);
    
           // Reads a single line from the console
           // and stores into name variable
           name = input.nextLine();
    
           // Reads a integer from the console
           // and stores into age variable
           age=input.nextInt();
           input.close();
    
           // Prints name and age to the console
           System.out.println("Name :"+name);
           System.out.println("Age :"+age);
    
        }
    }
    Last edited by mainy; 03-18-2009 at 05:59 PM.

  12. #12
    ezee is offline Member
    Join Date
    Mar 2009
    Posts
    14
    Rep Power
    0

    Default

    no-no , I know all the basics of class and object but don't know about mechanism of importing class or(even i don't know it is class or what)
    please read my previous post again, emphasize on bold letters.

  13. #13
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Yes ezee, you got your second conclusion right.
    B'coz this code is making a "in" object[Scanner in = new Scanner(-do-)]. So, i assume Scanner as a class ...
    The Scanner is a class and the methods (functions) that you can use in the scanner class include nextLine() ....

    "java.util" This is among the packages offered by java. I think one thing you should take a look at is the API (Application Programming Interface), I am not sure if you have but it's very useful and your number ONE (might be number two when it comes to syntax)reference when creating applications.

    Java 2 Platform SE 5.0

    As for your first question, the "in" is a FIELD contained in the System class. According to the Java API it says
    "in

    public static final InputStream in

    The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user. "

    I used to think the "in" was a method :) until you asked.

Similar Threads

  1. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 02:39 PM
  2. I need help fixing my code.. or non code?
    By MrHuggykins in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 11:12 PM
  3. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •