Results 1 to 6 of 6
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Character.isLetter problem

    I am trying to work with converting integers to string for the purposes of ending loops. The code below is what I am working with. The idea is that the user can input deposits until the user enters a negative value or the letter "Q". At that point the total deposits are returned to the calling procedure. The code ran fine until I included the Character.isLetter('Q') in the if statement. As of now, this line always returns True and never gets to the else. Thank you for your help.

    [ code]
    import java.util.*;
    public class Deposits {

    public static int getDeposit(int ttlDep){
    int value = 0;


    System.out.println("Enter a negative number or Q when finished entering deposits");
    Scanner input = new Scanner(System.in);



    value = input.nextInt();
    String aString = Integer.toString(value);


    System.out.println("aString is: " + aString);

    if ((value <= 0)||(Character.isLetter('Q')))
    return ttlDep;
    else
    ttlDep = ttlDep + value;
    return getDeposit(ttlDep);

    }

    public static void main(String[] args){
    int finalDep = 0;
    System.out.println("Enter your deposit below.");
    int dep = getDeposit(finalDep);
    System.out.println("Your deposit of $" + dep + " was received. Thank you");

    }
    }
    [/code]

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    8

    Default

    As of now, this line always returns True and never gets to the else.
    no wonder if you write it hard coded with constant character :)

    || (Character.isLetter('Q') --> is every time true, you don`t use the input !
    ->
    || (Character.isLetter('HERE THE ENTERED CHARACTER FROM THE USER!')

  3. #3
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default Tried but not getting it

    The text value aString was converted from int to string to store the character the user input. When I put that value in the Character.isLetter(aString), I get a InputMismatchException. Isn't that what you were suggesting? I also tried Character.isLetter('aString') and then isLetter is identified as the error.

    Can you help a little bit more please?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    6

    Default

    you seem to have missed out an opening bracket after your IF statement...
    Java Code:
    if ((value <= 0)||(Character.isLetter('Q')))
    return ttlDep;
    else
    ttlDep = ttlDep + value;
    return getDeposit(ttlDep);

    The 'else' part will not be run because the IF statement is closed after 'return ttlDep;'.

    Java Code:
    if ((value <= 0)||(Character.isLetter('Q'))) {
      return ttlDep;
    } else {
      ttlDep = ttlDep + value;
      return getDeposit(ttlDep);
    }

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Quote Originally Posted by ozzyman View Post
    The 'else' part will not be run because the IF statement is closed after 'return ttlDep;'.
    Actually the reason has already been posted above.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Quote Originally Posted by BillyB View Post
    The text value aString was converted from int to string to store the character the user input. When I put that value in the Character.isLetter(aString), I get a InputMismatchException.
    The reason is because the isLetter method expects a char as the parameter but you are passing a String. You don't even need the isLetter method. Use the equals method of String.
    Java Code:
    if(input.equalsIgnoreCase("Q")) {
        user wants to quit.
    } else {
        convert input to an int
    }

Similar Threads

  1. Replies: 5
    Last Post: 03-12-2011, 02:04 AM
  2. Replies: 2
    Last Post: 02-24-2009, 05:32 PM
  3. Replies: 1
    Last Post: 01-15-2008, 09:55 PM
  4. Problem with display the character
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 07:43 PM
  5. Problem with '/' character in HTML and JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-04-2007, 05:32 AM

Tags for this Thread

Posting Permissions

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