Results 1 to 15 of 15

Thread: [RESOLVED] Getting input from a user

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Resolved [RESOLVED] Getting input from a user

    Im just learning the basics of Java.Im stuck at something.

    How do I get an input from a user.Eg-If I want to get a number(integer),I tried using BufferedReader,but isnt there an easier way,like for output I use System.out.println,is there something similar to input?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Getting input from a user

    You can instantiate a "java.util.Scanner" object it can read all kinds of input
    p.s. Scanner exists for java 5 only
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Getting input from a user

    Search for System.in


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Getting input from a user

    Im a bit confused with the answer's,My question is: Can System.in command be used in any way to get input from the user?

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Getting input from a user

    Code:
    Scanner s=new Scanner(System.in);
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Getting input from a user

    In several ways.. The Scanner is 1.5

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String crap = br.readLine();

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Getting input from a user

    ok,after declaring the buffered reader,how do I get it to read an integer or a float variable?

  8. #8
    Lively Member
    Join Date
    Jul 2002
    Posts
    86

    Re: Getting input from a user

    This is what you do:

    String temp;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    ......
    System.out.print("enter message here"); //prompt for user
    temp=br.readLine();
    .......

    Then if you need it as an integer value, use Integer.parseInt(temp). I think there is the same thing for the float class to, I could be wrong.

  9. #9
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Re: Getting input from a user

    You can try the following:

    VB Code:
    1. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    2. System.out.print("Enter Integer value : ");
    3. int x=Integer.parseInt(br.readLine()); //get user input and convert to Integer
    4. System.out.print("Enter Float value : ");
    5. float f = Float.parseFloat(br.readLine()); //convert to float
    6. System.out.print("Enter Double value : ");
    7. double d = Double.parseDouble(br.readLine());//convert to double

    But don't forget to import package java.io.*

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Getting input from a user

    Thanks,one final question,does Buffered Reader require an Exception or anything to be written(like extends or implements) in the function where I use it?

  11. #11
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Re: Getting input from a user

    BufferedReader is a class in java.io.*. The method readLine() of this class throws an execption. Here is a complete example.

    VB Code:
    1. import java.io.*;
    2. public class UserInput{
    3.   public static void main(String args[]) throws IOException{
    4.          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    5.         System.out.print("Enter Integer value : ");
    6.         int x=Integer.parseInt(br.readLine()); //get user input and convert to Integer
    7.         System.out.print("Enter Float value : ");
    8.         float f = Float.parseFloat(br.readLine()); //convert to float
    9.         System.out.print("Enter Double value : ");
    10.         double d = Double.parseDouble(br.readLine());//convert to double          
    11.   }
    12. }

  12. #12
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Getting input from a user

    I recommend in a try-catch clause.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Getting input from a user

    Thanks to all.

  14. #14
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: Getting input from a user

    Can't you just use .readInt()?
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  15. #15
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Re: Getting input from a user

    No you cannot use .readInt(). There is no readInt() method of BufferedReader.

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