The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem with input from keyboard

5 replies on 1 page. Most recent reply: May 6, 2004 5:41 AM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Sam Crickow

Posts: 4
Nickname: limpit23
Registered: Apr, 2004

Problem with input from keyboard Posted: Apr 30, 2004 4:01 PM
Reply to this message Reply
Advertisement
I have got a Mac OS X and is using JJEdit to write my programs. The following lines are not working :

import java.io.*;
class EchoString
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String message;
System.out.println("Enter a line of text:");
message = stdin.readLine();
System.out.println("You entered: \"" + message + "\"");
}
}

No error message is displayed. A blank screen appears and there is no response from the keyboard. I have tried similar simple programs in other forms & from many other sources but nothing works.

Hope somebody can help!


Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Problem with input from keyboard Posted: May 2, 2004 5:58 PM
Reply to this message Reply
heres a simple code for ya to try

package program5;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
/**
example for buffered input reader for text input
*/
 
public class BufferReader
{
  public static void main(String[] args)
  {
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    try
    {
    System.out.print("Enter a string integer number: ");
    int ans = Integer.parseInt(userInput.readLine());
    System.out.println("The user input is: " + ans);
    System.out.print("Enter a string name: ");
    String name = userInput.readLine();
    System.out.println("The name is: " + name);
    }
    catch (Exception e)
    {
    }
  }
}



Hope this helps


Meth

Sam Crickow

Posts: 4
Nickname: limpit23
Registered: Apr, 2004

Re: Problem with input from keyboard Posted: May 4, 2004 3:14 PM
Reply to this message Reply
Unfortunately, the problem stayed the same. No output on the screen. No input taken in. No "System.out.print" works after the line :

"BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));"

But output is still possible before the line.

Hope somebody can find a way out!

I really need to know how to input to create a good enough program.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Problem with input from keyboard Posted: May 4, 2004 8:14 PM
Reply to this message Reply
Try something like this.

import javax.swing.*;
class EchoString
{
  public static void main(String[] args)
  {
    String message = JOptionPane.showInputDialog(null, "Enter a line of text.");
    System.out.println("You entered: \"" + message + "\"");
    //or better yet
    //JOptionPane.showMessageDialog(null, "You entered: \"" + message + "\"")l
  }
}

Sam Crickow

Posts: 4
Nickname: limpit23
Registered: Apr, 2004

Re: Problem with input from keyboard Posted: May 5, 2004 2:11 PM
Reply to this message Reply
Thanks.

The program worked although it took about 5 seconds before I could start entering text. I would have preferred to avoid using javax though & to know why the previous codes did not work.

Thanks again.

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Problem with input from keyboard Posted: May 6, 2004 5:41 AM
Reply to this message Reply
> The program worked although it took about 5 seconds before
> I could start entering text. I would have preferred to
> avoid using javax though & to know why the previous codes
> did not work.
I've never used System.in the way that you are, so I'm not sure what the problem is. On the rare occasions that I get console input, I use a class that is specifically designed for it. Here is the readString() method out of that class. I did not write the class, but it was posted as open source, so it should be OK to use a fragment.

public static String readString()
{
      int     ch = 0;
      String  r = "";
      boolean done = false;
 
      while (!done)
      {
         try
         {
            ch = System.in.read();
            if (ch < 0 || (char)ch == '\n')
               done = true;
            else if ( (char) ch != '\r')
               r = r + (char) ch;
         }
         catch(java.io.IOException e)
         {
            done = true;
            return null;
         }
      }
      lastChar = ch;
      return r;
}

Flat View: This topic has 5 replies on 1 page
Topic: Switichin On\Off keyboard's LED display Previous Topic   Next Topic Topic: IIS and Websphere

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use