Results 1 to 4 of 4
  1. #1
    bige15 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default readint() in Console cannot be apllied to (java.lang.String)

    public class Hilo
    {
    public static void main(String[] args)
    {

    {
    do{
    int guess;
    int num;
    num=(int)(Math.random() *101+1);
    guess= Console.readInt("Take a guess.");
    if (guess>num)
    {
    System.out.println("Your guess is too high. \n Please guess lower.");
    }
    else if (guess < num)
    {
    System.out.println("Your guess is too low. \n Please guess higher.");
    }
    else
    System.out.println("Your guess is correct, good job.");

    System.out.println("Thanks for playing the Hi-Low guessing game.");
    ans=Console.readChar("Would you like to play again? (y/n)");
    }while (ans=='y');
    }
    }
    }


    The error reads readInt() in console cannot be applied to (java.lang.String)
    I am very new to java, so please explain in detail.

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Console?

    What is Console? It hasn't been define o stated in the porgram.

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    bige15 is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default console

    import java.io.*;

    /** The <B>Console</B> class consists of static methods that allow easy reading
    of the basic data types <TT>double</TT>, <TT>int</TT>, and <TT>String</TT>
    from standard input. <I>It must be placed in the same directory containing
    the source code using it.</I><P>

    <B>Usage Example:</B>
    <PRE>
    System.out.println("Enter a double number: ");
    double x = Console.readDouble();
    System.out.println("You have entered: " + x);
    </PRE>

    @author Bert G. Wachsmuth
    @version 98/03/10
    */

    public class Console
    {
    /** Reads a single double value from the keyboard. If there is an error, the
    program will halt.
    @return double
    */
    public static double readDouble()
    {
    String tmpX = readString().trim();

    try
    {
    return (new Double(tmpX)).doubleValue();
    }
    catch (NumberFormatException ne)
    {
    System.err.println("Not a double. Quitting ...");
    System.exit(-1);
    return 0.0;
    }
    }

    /** Reads a single int value from the keyboard. If there is an error, the
    program will halt.
    @return int
    */

    public static int readInt()
    {
    String tmpX = readString().trim();
    try
    {
    return Integer.parseInt(tmpX);
    }

    catch (NumberFormatException ne)
    {
    System.err.println("Not an integer. Quitting ...");
    System.exit(-1);
    return -1;
    }
    }

    /** Reads a String from the keyboard until RETURN or ENTER key is pressed.
    @return String
    */

    public static String readString()
    {
    String string = new String();
    try
    {
    int cc = System.in.read();
    while (!((cc == 13) || (cc == 10)))
    {
    string += (char) cc;
    cc = System.in.read();
    }
    }
    catch (IOException e)
    {
    }
    return string;

    }
    }

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default OK....

    Thanks for the info.... but is Console in a class file or a JAR file or something else? You have to import it or instanciate the class in your program, before you can use it

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 3
    Last Post: 12-01-2008, 05:40 AM
  2. Replies: 2
    Last Post: 09-18-2008, 07:30 AM
  3. Replies: 5
    Last Post: 01-13-2008, 10:36 PM
  4. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  5. Can't convert java.lang.String to int.
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-13-2007, 05:05 PM

Posting Permissions

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