Results 1 to 9 of 9
  1. #1
    karma is offline Member
    Join Date
    May 2007
    Posts
    6
    Rep Power
    0

    Default How to get input from Console

    I want to get input from console. Output is easy using System.out.print() but what about input?

  2. #2
    levent Guest

    Default

    You can use a BufferedReader to read a line from the "standard" input stream as follows:

    Java Code:
    BufferedReader reader = new BufferedReader(System.in);
    String input = reader.readLine();
    Alternatively, if you are using Java 6 or above, you can use Console class as follows:

    Java Code:
    Console console = System.console();
    String username = console.readLine("User: ");

  3. #3
    karma is offline Member
    Join Date
    May 2007
    Posts
    6
    Rep Power
    0

    Default

    Thanks Levent!

  4. #4
    Belete is offline Member
    Join Date
    Dec 2009
    Location
    AddisAbaba
    Posts
    10
    Rep Power
    0

    Default

    Hey guys i was trying to send file to a server from a client and the file will be sent but a serious of small rectangles which are equal to the array length i initialized are also displayed with the file i sent in the server side; what code should i use to avoid this, please can somebody help me out.
    this are the code i used
    Client side
    public void sendFile(){
    try{
    bis = new BufferedInputStream(new FileInputStream(jFileChooser1.getSelectedFile()));
    bos = new BufferedOutputStream(client.getOutputStream( ));
    byteArray = new byte[(int)jFileChooser1.getSelectedFile().length()];
    bis.read(byteArray,0, byteArray.length);
    bos.write(byteArray,0, byteArray.length);
    bis.close();
    bos.close();
    }

    Server side

    public void receiveFile(){

    try{
    byte receivedData[] = new byte[40];
    bis = new BufferedInputStream(socket.getInputStream());
    bis.read(receivedData);
    String st=new String (receivedData,0,receivedData.length);
    System.out.println(st);
    bos = new BufferedOutputStream(new FileOutputStream("belete.txt"));
    bos.write(receivedData);
    bis.close();
    bos.close();
    }

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    And how does that relate to getting input from the console? Why do you hijack a dead thread?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    Belete is offline Member
    Join Date
    Dec 2009
    Location
    AddisAbaba
    Posts
    10
    Rep Power
    0

    Default

    it is not about getting input from console its just the file transfer system, please help me out i really need it

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by Belete View Post
    it is not about getting input from console its just the file transfer system, please help me out i really need it
    Please don't hijack an unrelated thread; the title of the thread is misleading w.r.t. your subject; hijacking is considered rude, better start your own thread with an appropriate thread title.

    kind regards,

    Jos

  8. #8
    amharic45 is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default

    deleted by mod
    Last edited by Fubarable; 08-13-2010 at 10:32 PM. Reason: deleted by mod

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    29

Similar Threads

  1. How to read input from console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-16-2008, 11:57 PM
  2. How to read input from the console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-06-2008, 08:41 PM
  3. Console doesn't appear!
    By PeteMarsh in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 06:41 PM
  4. Taking input from console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:47 PM
  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, 09: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
  •