Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: New to Java, could you explain something to me?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java, could you explain something to me?

    Hi, I am new to java, and I have been following some youtube video tutorials and I'm not clear on something, so could you please explain it.

    Here is the code:

    import java.util.Scanner;
    public class GetUserInput {
    	public static void main(String args[]){
    	Scanner number = new Scanner(System.in);
    	System.out.println(number.nextLine());
    	}
    }

    On the line
    Scanner number = new Scanner(System.in);
    I am creating a variable called number of the Scanner class, and then I am assigning it something, this is what I'm confused with. I know the new keyword creates an instance of a class but I dont know what is being assigned to my number variable?

    Thanks.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: New to Java, could you explain something to me?

    I recommend you read through the basic tutorials, especially this section: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: New to Java, could you explain something to me?

    new Scanner(System.in) is a call to the constructor for a Scanner object. What you're doing is creating a new Scanner object then assigning that object to the variable number (for a more verbose answer, the variable number "references" or "points to" that Scanner object that was created).

    The System.in is a parameter of the Scanner constructor. It's the stdin stream (i.e. the stream that console input is read from). It's a static field found in the class System, and the field's name is in.

  4. #4
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java, could you explain something to me?

    Thanks for replying. I have a few questions:

    1. So, in
    Scanner number = new Scanner(System.in);
    I am assigning number the reference to a Scanner object?

    2. With that being said, that means when I refer to the number variable I am using the scanner object?

    3. And then we are giving the Scanner object a parameter of our keyboard input, so when it comes to the line
    System.out.println(number.nextLine());
    it displays the values I type it, althought it pauses and waits until I type in values because none have yet been entered?
    Last edited by TP-Oreilly; September 16th, 2011 at 11:45 AM.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java, could you explain something to me?

    If I'm right could you put just a simple yes

  6. #6
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: New to Java, could you explain something to me?

    You are correct. Congrats.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java, could you explain something to me?

    Thanks

Similar Threads

  1. Pls explain me the code logic
    By Shajith in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2011, 02:41 AM
  2. Replies: 1
    Last Post: December 13th, 2010, 05:13 AM
  3. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 02:29 PM
  4. Help me this code! Someone please explain strings!
    By helpthiscode in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 16th, 2009, 03:13 AM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM