Results 1 to 2 of 2
Thread: Help with program input
- 07-05-2007, 04:57 AM #1
Member
- Join Date
- Jun 2007
- Posts
- 91
- Rep Power
- 0
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:
The basic idea is to accept user input while reading from a text file.Java 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"); } }
Thanks
Daniel:o
- 07-05-2007, 05:02 AM #2
Senior Member
- Join Date
- Jun 2007
- Posts
- 114
- Rep Power
- 0
So let's look at your file input
You create a BufferedReader object, that takes in a Reader of some type.Java Code:BufferedReader br = new BufferedReader (new FileReader ("plus.txt"));
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.
GreetingsJava 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(); } } }
Albert:rolleyes:
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
beginner needs help with OBD-II input
By andrewos in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:46 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks