JAVA – a program that returns true if character entered is a letter, (please help me with my code)?

JAVA – a program that returns true if character entered is a letter, (please help me with my code)?
JAVA – a program that returns true if character entered is a letter, (please help me with my code)?
How do I do this? Write a boolean method isLetter that returns true if and only if its single parameter is a letter of the alphabet (either upper case or lower case).

import java.util.*;
class LetterOfAlphabet
{
public static void main (String[] args)
{
Scanner get = new Scanner(System.in);
System.out.println(“Will you enter a character”);
char response =get.next().charAt(0);
isLetter(response);
if (true)
System.out.println(“yes”);
else
System.out.println(“no”);

}

public static boolean isLetter(char i)
{
int charInt = (int)i;

if((charInt >= 65 && charInt <=90) || (charInt >=97 && charInt <=122) )
return true;
else
return false;
}
}

I can't figure how to i guess you would say "change the int back into a char" and make the output be yes if its a letter, but no if its like a string, num, symbol, etc. Someone please help cause it says YES to everything i type in

Best answer:

import java.util.*;
class LetterOfAlphabet
{
public static void main (String[] args)
{
Scanner get = new Scanner(System.in);
System.out.println(“Will you enter a character”);
char response =get.next().charAt(0);
if (isLetter(response))
System.out.println(“yes”);
else
System.out.println(“no”);

}

public static boolean isLetter(char i)
{
return i >= ‘A’ && i <= 'Z' || i >= ‘a’ && i <= 'z';
}
}

This program is tested and works.

Tags: , , , , , , , , ,

Under Forum

2 Comments for JAVA – a program that returns true if character entered is a letter, (please help me with my code)?

  • 1. Blackcompe  |  January 2nd, 2008 at 7:56 pm

    you could also use Character.isLetter.

  • 2. %HOme/noT  |  January 2nd, 2008 at 8:21 pm

    public boolean isLetter(char x)
    {
    return (((int) x > 64) && ((int) x < 91) || (((int) x >96) && ((int) x <122)));
    }

Leave a Comment for JAVA – a program that returns true if character entered is a letter, (please help me with my code)?

Required

Required, hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Forum