Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2007, 05:57 AM
Member
 
Join Date: Jun 2007
Posts: 92
Daniel is on a distinguished road
Help with program input
Hi I'm having some trouble trying to work out how to accept user input from the keyboard.

I would really like to get the hang of Java but I'm finding it very difficult so any help would be greatly appreciated.

What I have done so far:

Code:
import java.io.*; import java.util.*; /** * Used to recieve input from the user. * * Cameron Lett * 8/4/07 */ public class Input { /** * Constructor for objects of class input */ public static void main (String[] args) throws java.io.IOException { String b1; String b2; // set up the buffered reader to read from the keyboard BufferedReader br = new BufferedReader (new FileReader ("plus.txt")); b1 = br.readLine(); System.out.println ("Enter a Number"); } }
The basic idea is to accept user input while reading from a text file.
Thanks

Daniel
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-05-2007, 06:02 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
So let's look at your file input
Code:
BufferedReader br = new BufferedReader (new FileReader ("plus.txt"));
You create a BufferedReader object, that takes in a Reader of some type.
This time you've decided to use a FileReader in order to get the information inside of plus.txt

Well with Keyboard input you can do basically the exact same thing.
Instead of using a FileReader, however, you want to use what is called an InputStreamReader.
This is used to get the input stream device and put it into the bufferedreader.

An example I quickly coded up is shown below, including weak exception handling.
Code:
import java.io.*; public class test { public static void main (String args[]) { try { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input Some String: "); String line = stdin.readLine(); System.out.println("You typed: " + line); stdin.close(); } catch (IOException io) { System.err.println("IO Exception Caught"); io.printStackTrace(); } } }
Greetings
Albert
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
input placed in array smilejava New To Java 1 11-05-2007 01:32 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
beginner needs help with OBD-II input andrewos New To Java 3 07-30-2007 10:46 AM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 09:46 AM


All times are GMT +3. The time now is 03:00 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org