Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Why isn't this working?? String number = console.readLine("Enter your favorite number: "); if(number.isLetter())...

Code:

import java.io.Console;

public class Isaiah {

    public static void main(String[] args) {
        Console console = System.console();
        String isName = console.readLine("Enter your name: ");
        String fatal = "";

        if (isName.equals("*ISAIAH*")) {
            String secretCode =console.readLine("YOU HAVE ENTERED A SECRET CODE.\nHERE ARE YOUR OPTIONS.\n >IF YOU WANT TO CONTINUE:\nPRESS I.\n >IF YOU WANT TO EXIT:\n PRESS  E.\n  ");       

            if (secretCode.equalsIgnoreCase("E")) {
                System.exit(0);
            } else if (secretCode.equalsIgnoreCase("I")) {
                fatal = console.readLine(" WARNING:\nTHIS PROGRAM CAN BE FATAL.\nDO YOU STILL WANT TO     CONTINUE?\n\n  ");

                if(fatal.equalsIgnoreCase("yes")) {
                    console.readLine("Hello, %s.\n", isName);
                } else {
                    System.exit(0);
                }
            }
        }

        String number = console.readLine("Enter your favorite number:  ");

        if (number.isLetter()) {
            throw new IllegalArgumentException("A number is required.");
        }

        String mood = console.readLine("Enter your favorite mood:  "); 
        String creature = console.readLine("Enter your favorite creature:  ");
        String color = console.readLine("Enter your favorite color:  ");
        String lotsOfStuff = mood;      
        lotsOfStuff = lotsOfStuff.toLowerCase();
        String moreLotsOfStuff = color;
        moreLotsOfStuff = moreLotsOfStuff.toLowerCase();
        String evenMoreLotsOfStuff = number;
        evenMoreLotsOfStuff = evenMoreLotsOfStuff.toLowerCase();
        String evenEvenMoreLotsOfStuff = creature;
        evenEvenMoreLotsOfStuff = evenEvenMoreLotsOfStuff.toLowerCase();
        String isFavoriteThing = console.readLine("Is a %s, %s %s year old %s your favorite thing?  ", lotsOfStuff, moreLotsOfStuff, evenMoreLotsOfStuff, evenEvenMoreLotsOfStuff);

        if (isFavoriteThing.equalsIgnoreCase("no")) {
            console.readLine("Why not?  ");
            console.printf("Well you should, it's all of your favorite stuff.\n\n");
        }

        String computer = console.readLine("-------------------------------------------------\n\n  Do you want to have a talk with the computer?  ");
        console.printf("\n---------------------------------------------\n\n\n");
        String howYouDoing = "";
        if(computer.equalsIgnoreCase("yes")) {
            howYouDoing = console.readLine("Hello %s, how are you doing?\n  ", isName);

            if(howYouDoing.equalsIgnoreCase("sad")) {
                console.readLine("Why are you %s, %s?  \n", howYouDoing, isName);
            }
        }
    }
}

Error:

Isaiah.java:15: error: cannot find symbol                                                                                                                                                                        
if(number.isLetter()){throw new IllegalArgumentException("A number is required.");}                                                                                                                              
         ^                                                                                                                                                                                                       
  symbol:   method isLetter()                                                                                                                                                                                    
  location: variable number of type String                                                                                                                                                                       
1 error

Hi Isaiah,

Fixed markdown for the posted code. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.

The language specifier had a dot (".") in front of it, preventing syntax highlighting and none of the code was indented, making it very difficult to read.

Cheers!

1 Answer

You are getting an error because your number variable is of type String. The [isLetter](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isLetter(char\)) method is part of the Character class.

If you want to check whether a digit is present within your string, I recommend you use the [matches](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches(java.lang.String\)) method. Here is an example:

public class Introductions {

    public static void main(String[] args) {
        String number = "1";
        String words = "Hello!";

        if (number.matches("\\d")) {
         System.out.println("Success!"); 
        }
        if (words.matches("\\d")) {
          System.out.println("Oh, no!");
        }
  }
}

can I make isLetter be of type string? :smiley: