Results 1 to 8 of 8

Thread: Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    90

    Question

    is this the way that you input information into a program from a keyboard
    Code:
    Temperature = Keyboard.readint();
    cause it does not seem to work

    dooes anyone know another way to input information from a keybord in java
    CaM

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    No

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    90
    how do you do it then???
    CaM

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry i was just having a little fun. Do you want to read input from the console? The following code just reads in what you typed at the command line and spits it back out, then terminates.
    Code:
     import java.io.*; 
     
     public class Q{
      public static void main(String[] agrs){
      
      try{
       BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
       System.out.println(buff.readLine()); 
      }catch(IOException e){
        System.out.println(e);
       }
      }
     }

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    90
    yup that is what i need thanks
    CaM

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    90
    actually How would i put that lets say number into a variable that i made
    CaM

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    readLine() returns a String. So the following would store the string returned into the s variable which has a type of String. String s = buff.readLine(); If you want to store the returned String into a integral type you have to use the parseType(String s) methods found in the Wapper classes such as java.lang.Integer. Then you could do somthing like the following. int i = Integer.parseInt(buff.readLine()); but be prepared to catch a NumberFormatException incase what is being passed into the parseType() method is not a integral representation.

Posting Permissions

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



Click Here to Expand Forum to Full Width