-2

So I just started my Java class and I'm having a bit of trouble understand this. I have googled and looked in my book but I am just not getting it.

So my project starts out with

// declare an instance of Scanner to read the data stream from the keyboard.

Scanner kb = new Scanner(System.in);

Then the next line is

// say hello to the user and ask for the user’s name
    System.out.print("Hello, please enter your name: ");
    name = kb.nextLine();

Can I just get a really simple explanation or breakdown of what the first line which is creating a new scanner, what that does. And then what the

name = kb.nextLine();

What does that do, what is it referencing or creating. I am just really lost. Thanks in advance for all the help

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
archer
  • 89
  • 8
  • `Scanner` is a class which is used to parse/read text based input from a variety of different sources. In your case, it's establishing a `Scanner` which reads from the programs "standard input" (in most cases the keyboard). `kb.nextLine` is waiting until a new line of text has been made available to the `Scanner` via the "standard input", so basically, it's waiting until you press the [Enter] key and will read the entire line of text you typed before pressing [Enter] – MadProgrammer Sep 21 '15 at 00:49
  • 1
    Did your teacher not explain to you what this is? – D. Ben Knoble Sep 21 '15 at 00:54
  • You may find this helpful - [Difference between object and instance](http://stackoverflow.com/questions/3323330/difference-between-object-and-instance?lq=1). You have "declare an instance" in that comment, so knowing what "an instance" means might be a good starting point. `kb` is your instance of the class `Scanner`, `nextLine` is the method of your instance which you are calling. To know what that does, consult [the docs](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) (some IDEs might also tell you). – Bernhard Barker Sep 21 '15 at 01:13
  • But honestly, if you're asking us this question, I'd say you're not learning the language correctly. Any half-decent book, teacher, tutorial or course should start off explaining these core concepts to you (you should ask your teacher to explain them if they don't, or ignore the teacher and just go read a decent Java book, which may or may not be the one prescribed to you - Google for recommendations). – Bernhard Barker Sep 21 '15 at 01:26

2 Answers2

1

The Scanner class is with in the java.util package. When you write

  Scanner kb = new Scanner(System.in);

You are declaring a Scanner reference variable called kb, the Scanner constructor takes an InputStream. There are other acceptable parameters. System.in is an InputStream, this allows input.

Refer to http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

When the user provides input (usually via a keyboard/series of key events) and presses enter the input is read.

 name = kb.nextLine();

The Scanner class has many instance methods, which are of the form next*****(). In the case of nextLine():

Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line

RamanSB
  • 1,162
  • 9
  • 26
1

If you need to construct a wall for example, you will need cement. Maybe you have it or you need to purchase. Either way, you have to make it available to you for you to use it.

Instantiation makes it available to you by creating a new one. Particularly the new keyword does it.

The same way, you are creating a Scanner object-

Scanner kb = new Scanner(System.in);

This object can scan/read texts from some standard input.

name = kb.nextLine();

nextLine is a method defined in Scanner class that waits until the current line is available meaning when it hits 'end of line' or 'end of stream', it scans the current line. By pressing the Enter key on your keyboard while on console, you make the current line available to the Scanner. Everything that had been typed before pressing the Enter key would be available and you are simply storing it to the name.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74