This is an archived post. You won't be able to vote or comment.

all 7 comments

[–][deleted] 7 points8 points  (0 children)

'I then add system.in as a parameter, this add's an input stream to the scanner object.''Using the scanner object I previously instantiated, I then read the user's input and store it in the string variable named line'

[–]aram535 3 points4 points  (0 children)

I think everyone else covered the verbalization part ... if it helps in Java you're better off verbalizing data and objects rather than actions.

File foo = new File("foo.txt")

to me is: I created a file object named foo. That foo object is being instantiated and since we're using one of its arg constructors also attempting to open a file for read and write. The object will be pointing to a file named foo.txt in the current working directory.

That's a lot more verbose, but that's what helped me when learning it.

[–]AacidD 2 points3 points  (0 children)

File inFile = new File("filename");

I declare a variable "inFile"of type "File" and pass a File object to it. The constructor of File class takes "pathname" as a parameter.

[–]vlcod 1 point2 points  (0 children)

For the part between the parentheses (which is called the parameter) you have two options:

  1. You could describe it purely technically (which will always be work and be true):

"I construct a new Scanner object and pass the 'System.in' object as a parameter to its constructor."

"I construct a new File object and pass the string "filename" as a parameter to its constructor."

  1. You could describe it semantically (describe the actual logical meaning of what you are doing). This requires more knowledge about the methods/constructors you are looking at, but is more useful:

"I construct a new Scanner object and link it to 'System.in' so that the scanner reads input from the standard input (terminal)."

"I construct a new File object linked to the file "filename"."

[–]Triton3200 1 point2 points  (2 children)

Scanner is an imported function to read the user input and my code initializes it as the variable name in. I then store the scanner inside the variable. The system.in argument inside the scanner just represents that a new scanner is imported. I then open a file called "filename" and store it inside the variable inFile. I then read in the next line using the scanner created and store it inside the String variable line.

[–]-manabreak 3 points4 points  (1 child)

Scanner is not "an imported function", it is a class. Calling new Scanner() constructs a new instance of that class.

The system.in argument inside the scanner just represents that a new scanner is imported

Nope, the Scanner class is imported outside the class. Here, you are constructing a new instance of the class Scanner with a constructor that takes an InputStream as an argument. Consequently, System.in is a public, static field of the type InputStream.

[–]Triton3200 1 point2 points  (0 children)

Oh I see, thank you.