Results 1 to 2 of 2
  1. #1
    Daniel is offline Member
    Join Date
    Jun 2007
    Posts
    91
    Rep Power
    0

    Default 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:

    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");
    }
    }
    The basic idea is to accept user input while reading from a text file.
    Thanks

    Daniel:o

  2. #2
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    So let's look at your file input
    Java 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.
    Java 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:rolleyes:

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  2. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 12:32 PM
  3. Replies: 0
    Last Post: 10-04-2007, 09:33 PM
  4. beginner needs help with OBD-II input
    By andrewos in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:46 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •