We help IT Professionals succeed at work.

TerminalIO.KeyboardReader problem

macmaya
macmaya asked
on
1,807 Views
Last Modified: 2007-12-19
Dear experts,

I am a newbie of Java and I am using Mac for the development.

My simple program:

public class Convert {
      public static void main (String [] args) {
            KeyboardReader reader = new KeyboardReader();
            double fahrenheit;
            double celsius;
            
            System.out.print("Enter degrees Fahrenheit: ");
            fahrenheit = reader.readDouble();
            
            celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
            
            System.out.print("The equivalent in Celsius is ");
            System.out.println(celsius);
            
            reader.pause();
      }
}

When the program is compied, it got the following error message, please kindly help :)


root:~/introcs/hello root# javac Convert.java
Convert.java:3: cannot resolve symbol
symbol  : class KeyboardReader
location: class Convert
                KeyboardReader reader = new KeyboardReader();
                ^
Convert.java:3: cannot resolve symbol
symbol  : class KeyboardReader
location: class Convert
                KeyboardReader reader = new KeyboardReader();

Best regards,
MacMaya
Comment
Watch Question

Where do you have the class KeyboardReader ?  It should be in your class path.  If it is in any package then you should import that package

Author

Commented:
Dear William,

i have try to import the package TerminalIO;
but i still failed, I am using java version "1.4.1_01

Best regards,
MacMaya
I dont think that KeyboardReader is a class available in java

Try to get the source from the following link and compile it in the same folder
http://www.doc.ic.ac.uk/~mrh/121/KeyboardReader.java.html

Author

Commented:
Hi William,

I added the texts you gave me but it still doesn't work, many thanks.

regards,
MacMaya
try this, You can also have the KeyboardReader class in a separate java file.

import java.io.*;

public class Convert {
     public static void main (String [] args) {
          double fahrenheit;
          double celsius;
         
          System.out.print("Enter degrees Fahrenheit: ");
          fahrenheit = KeyboardReader.readDouble();
         
          celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
         
          System.out.print("The equivalent in Celsius is ");
          System.out.println(celsius);
         
          KeyboardReader.pause();
     }
}

class KeyboardReader {

   /**
    * Dummy constructor to inhibit documentation.
    * <p>
    * This class cannot be used to instantiate objects.
    */
   private KeyboardReader() {}

   /**
    * Returned by the <i>readInt()</i> method to indicate EOI.
    */
   public static final int EOI_INT = Integer.MAX_VALUE;

   /**
    * Returned by the <i>readDouble()</i> method to indicate EOI.
    */
   public static final double EOI_DOUBLE = Double.MAX_VALUE;

   /**
    * Returned by the <i>readString()</i> method to indicate EOI.
    */
   public static final String EOI_STRING = "END_OF_INFO_1234";

   /**
    * Returned by the <i>readInt()</i> method to indicate an error.
    */
   public static final int ERROR_INT = Integer.MIN_VALUE;

   /**
    * Returned by the <i>readDouble()</i> method to indicate an error.
    */
   public static final double ERROR_DOUBLE = Double.MIN_VALUE;

   /**
    * Returned by the <i>readString()</i> method to indicate an error.
    */
   public static final String ERROR_STRING = "I/O_ERROR_1234";

   /**
    * Controls the output of error messages to the console in
    * response to inappropriate input.
    */
   public static boolean ERROR_MESSAGES = true;

   private static BufferedReader reader = new BufferedReader
                                        (new InputStreamReader(System.in));
   
   /**
    * Reads a line of input and converts it into an <i>int</i>.
    * @return the integer that the user typed, or
    *         EOI_INT to indicate end-of-information, or
    *         ERROR_INT to indicate a conversion or I/O error.
    */
   public static int readInt() {
      String s = "";
      try {
         s = reader.readLine();
      } catch(IOException e) {
         if (ERROR_MESSAGES)
            System.out.println("IO error: " + e);
         return ERROR_INT;
      }
      if (s == null)
         return EOI_INT;
      int n;
      try {
         n = Integer.parseInt(s.trim());
      } catch (Exception e) {
         if (ERROR_MESSAGES)
            System.out.println (
                        "Bad input: enter digits without decimal point");
         return ERROR_INT;
      }
      return n;
   }
   
   /**
    * Reads a line of input and converts it into a <i>double</i>.
    * <p>
    * @return the number that the user typed, or
    *         EOI_DOUBLE to indicate end-of-information, or
    *         ERROR_DOUBLE to indicate a conversion or I/O error.
    */
   public static double readDouble() {
      String s = "";
      try {
         s = reader.readLine();
      } catch(IOException e) {
         if (ERROR_MESSAGES)
            System.out.println("IO error: " + e);
         return ERROR_DOUBLE;
      }
      if (s == null) {
         return EOI_DOUBLE;
      }
      double x;
      try {
         x = Double.parseDouble(s.trim());
      } catch (Exception e) {
         if (ERROR_MESSAGES)
            System.out.println ("Bad input: enter digits only");
         return ERROR_DOUBLE;
      }
      return x;
   }
   
   /**
    * Reads a line of character input.
    * <p>
    * @return the line of input that the user typed, or
    *         EOI_STRING to indicate end-of-information, or
    *         ERROR_STRING to indicate a conversion or I/O error.
    */
   public static String readString() {
      String s = "";
      try {
         s = reader.readLine();
      } catch(IOException e) {
         if (ERROR_MESSAGES)
            System.out.println("IO error: " + e);
         return ERROR_STRING;
      }
      if (s == null)
         return EOI_STRING;
      return s;
   }

   public static void pause() {
         System.out.println("Press enter to exit...");
         try {
         reader.read();
      } catch(IOException e) {
         if (ERROR_MESSAGES)
            System.out.println("IO error: " + e);        
      }
   }
}

Author

Commented:
Hi William,

Thanks for your great help but ... stupid as i am, the program still does not work properly, may i email the file to you if you have time?

Thanks a lot.
Regards,
MacMaya
It is not leagal to solve problems through emails,

What problem do you have.  I have given you the complete java source.  You can just copy and save it as Convert.java and run it.  Submit what error are you getting...
Principal Technologist
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Dear William and mayankeagle,

Many thanks for both of your great help :)

Best regards,
MacMaya
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Glad to....

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.