Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

console input  RSS feed

 
Glen Bland
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody show me an example of code to input a string from the console window. I can take an argument to the program call as input, but I don't know how to get input once the program has started. I have a book that mentions creating a Scanner object and using it's next() method with System.in to get the next string. I can't get my programs to recognize Scanner though. I'm using the newest j2sdk1.4.2_12. any suggestions?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Glen,

Welcome to JavaRanch!

Scanner is a feature of JDK 1.5, which is now actually a few years old; JDK 1.4 is old-timer stuff by now. 1.6 is actually in late beta!

If you're stuck with 1.4 and can't upgrade for some reason (but you should, you really should...) then you can use BufferedReader to read lines of input. If you need to read numbers, you'll have to parse them out by hand, which is also a pain. But reading a line of text looks something like



That "1" in the first line is quite important, but many books forget to tell you about it. Without it, your program may appear to "hang" occasionally for reasons that we don't need to go into right now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!