• 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How do I....?

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in Java. I am trying to figure out how to read input from the command prompt. How do I acomplish this?

Hector :roll:
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Hector!

Around these parts, I normally prefer to help nudge a greenhorn to learn to solve problems for themselves, and point out things like the J2SE API documentation to help them on that path.

For a brand new programmer, getting console input is a little bit of a not-so-obvious chore, unless you've taken the time to study the basics of the language and the "I/O: Reading and Writing (but no 'rithmetic)" lesson of Sun's Java tutorial. Even then, the answer might not be obvious. So, here's the basic solution:I'll take a shot, here, at describing what the bold line of code is doing. In Java, data input and output is typically done in one of two ways: as bytes, or as characters. InputStreams and OutputStreams handle bytes. Readers and Writers handle characters.

System.in, that piece on the far right of the bold line, is an InputStream that can read bytes of data in from the system console. You could use it directly to obtain user input in the form of bytes, but then you'd have a bit of work in order to do to deal with a bunch of of bytes.

The InputStreamReader, which the example uses to wrap System.in, "is a bridge from byte streams to character streams," as the class documentation describes. The InputStreamReader provides a way to read a character from all those bytes provided by System.in, the InputStream. An InputStreamReader isn't the easiest thing to work with directly, though, since it only reads in data either one character at a time, or into a character array. That's where the BufferedReader makes things a little easier.

A BufferedReader can use an InputStreamReader (any type of Reader, actually) to read in a whole line of characters at a time, in the form of a String, which can be somewhat easier to work with, compared to what the previous two streams were offering: either bytes of data, or characters of data.

So, do you think you're beginning to get an idea of what's going on in this example program?

Since you're new to all this, I thought you might like to have this list of free on-line Java tutorials and books that I have found useful:
  • Sun's Java Tutorial
  • Introduction to Computer Science using Java by Bradley Kjell
  • Introduction to Programming Using Java by David J. Eck
  • Dick Baldwin's Java Programming Tutorials
  • Interactive Programming In Java by Lynn Andrea Stein
  • Bruce Eckel's Thinking In Java
  • JavaRanch's own Campfire Stories
  • Allen B. Downey's How To Think Like A Computer Scientist
  • If you have more questions, don't hesitate to ask. Folks around here are pretty good at helping.

    Good luck.
    [ September 26, 2004: Message edited by: Dirk Schreckmann ]
    I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
      Bookmark Topic Watch Topic
    • New Topic