• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Getting input from user

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there people, I just started learning Java and something so easy made me very confused because of its syntax and how other people did it online. I don't know why but I have other programming language experiences and getting input from user never confused me as Java did. Below example works perfectly fine and I just want you guys to confirmed that it is the correct way of doing it. In some videos people were  doing it little differently.

My purpose is to get users name and age by using string and int with same Scanner, and then print it out.

 
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

That's the right way to use the Scanner class but you should use better names. Look at the first line of code as a way to create a representation of the standard input stream. In C/C++, that would be stdin.

This might make more sense:
 
Saloon Keeper
Posts: 7175
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great for a person with a background in older languages or the underpinnings of operating systems. For the casual beginner I find this makes more sense.
 
Junilu Lacar
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gulent wrote:In some videos people were  doing it little differently.


What were they doing differently? Show us an example of what was different so we can comment.

There are a few different ways you can set up a Scanner. You can even set up multiple Scanners, one for each type of input (e.g. a file, a String, a Stream).

When creating a Scanner around System.in, however, you only need one in the whole program. That is, you can do this:


This way, stdin will be accessible anywhere in your application. You could also choose a name like keyboard or input instead. Also, by convention, names of final variables are capitalized so STDIN, KEYBOARD, or INPUT are also acceptable, maybe even expected.

Alternatively, you can add another layer of abstraction and use a utility class to represent input operations. Some folks here have their own utility classes called Keyboard or something like that. I'm sure someone will provide a link or example (cue Carey and/or Campbell...)
 
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I just name my Scanner instances "scanner". Or "myScanner". I definitely wouldn't name one "stdin", since it implies a tie to System.in that might change if you update or re-use the code. While a Scanner can use stdin as a data source, it actually works on a stream input, so the semantics of stdin and Scanner are quite different.

Plus, I could get myself into real trouble if I started naming things "stdin" and then went back to work on a platform like C where stdin has a predefined value.
 
Junilu Lacar
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:I definitely wouldn't name one "stdin", since it implies a tie to System.in


Well, if you assign new Scanner(System.in) to a variable named stdin, I'd say the implication is quite appropriate, isn't it? If you change that in the future, I'd hope you'd also change the name to something else that's more appropriate. With today's IDEs, refactoring a name is a trivial operation. I'm not a big fan of the name keyboard because input can easily be piped, e.g. from a file or from the output of another program, on the command line so the input isn't really coming from a keyboard. These are minor issues though. A Scanner named name is a bigger issue.
 
Mike Gulent
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I know it was a bad name but since I was just messing arround, I didn't really pay attention. On the other hand though, I think I'd go with "Keyboard" as well.
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Gulent wrote:. . . since I was just messing arround, I didn't really pay attention. . . .

Careful. If you get into sloppy habits for tiny programs, those sloppy habits can reappear elsewhere. And any error you introduce into your tiny programs can come and bite you when you try to use those tiny programs elsewhere. Write every program with the same level of care you would use for big programs.
And learn to use JShell if you simply want to try out a five‑line program and forget it afterwards.

And, again, welcome to the Ranch
 
Tim Holloway
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Tim Holloway wrote:I definitely wouldn't name one "stdin", since it implies a tie to System.in


Well, if you assign new Scanner(System.in) to a variable named stdin, I'd say the implication is quite appropriate, isn't it? If you change that in the future, I'd hope you'd also change the name to something else that's more appropriate. With today's IDEs, refactoring a name is a trivial operation. I'm not a big fan of the name keyboard because input can easily be piped, e.g. from a file or from the output of another program, on the command line so the input isn't really coming from a keyboard. These are minor issues though. A Scanner named name is a bigger issue.



No, I would not say that the implication was appropriate. A scanner is a construct that produces tokens. Stdin is a stream of characters. Equating one to the other is not a good way to develop rigorous thought, coding and design habits. And if you hand the project off to a junior programmer who doesn't comprehend this false equivalence, it's going to cost.

As for changing the name when the source changes, code re-use is predicated on being able to transfer existing assets from one project to another with as little modification as possible, both to reduce work and to reduce the possibility of introducting bugs. I've seen quite enough grief just from refactoring Hungarian Notation. Don't need more when the issue could have been nipped in the bud.
 
Junilu Lacar
Marshal
Posts: 15630
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess we'll just have to disagree on this.

I don't see a huge risk of many programmers seeing a declaration like Scanner stdin and confusing the variable for a Stream<Character> or in getting confused with what language they're working in. If they do, there are more serious issues I'd need to be talking about with them. Regarding reuse, I'd say that's what the utility classes that Campbell, Carey, and others have written would come in but I doubt there's much plug and play reusability in code that processes the command line arguments for a particular program.
 
Tim Holloway
Saloon Keeper
Posts: 22112
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I guess we'll just have to disagree on this.

I don't see a huge risk of many programmers seeing a declaration like Scanner stdin and confusing the variable for a Stream<Character> or in getting confused with what language they're working in. If they do, there are more serious issues I'd need to be talking about with them. Regarding reuse, I'd say that's what the utility classes that Campbell, Carey, and others have written would come in but I doubt there's much plug and play reusability in code that processes the command line arguments for a particular program.



https://www.gnu.org/software/libc/manual/html_node/Parsing-Program-Arguments.html

For Java:

http://commons.apache.org/proper/commons-cli/
http://args4j.kohsuke.org/
http://jcommander.org/

Among others.

But command line parameters don't come in via stdin, they come in via, um, the command line. Scanners are more likely to be used the lexical tokenizers for parsers, While a lot of people use Scanner to do simple data parsing, stuff like name=value format is really better left to operations like String.split(). Scanner shows its real strength when used, for example, in the front-end of a Java compiler when you have a complex and somewhat arbitrary mix of token types to return.

And circling around to command lines, a common Unix convention is a command in the format:

and variations on that theme - such as having the filename(s) be positionally last. Within that world is often a special filename: "-" that says instead of opening a named file, take the input from stdin. So a scanner named "stdin" would be especially confusing in apps like that.

Never underestimate the ability for people to get confused about obvious things, though. Why take a risk?
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic