Results 1 to 2 of 2
  1. #1
    imanoob is offline Member
    Join Date
    Oct 2013
    Rep Power

    Default How to check if a word is ascending?

    I'm trying to check if a word provided is ascending (ie. each letter <= the following letter, example: "ab" is ascending because a < b)

    for(int i=0; i<word.length(); i++) {
    boolean isAscending = true;
    if (word.charAt(i)>=i)
    //not ascending

    "word" is just the input word

    I know this is wrong because I don't know what I'm doing.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: How to check if a word is ascending?

    You are checking the character against its index, not against an adjacent character. And your while loop will never terminate as written.
    To determine relative order of adjacent letters assume you have the string "gold." You would need to do something like the following pseudo code:

    if (g < o) and if (o < l ) and if (l < d) then {
    // do something
    } else {
    // do something different

    Note: that you would want to use a loop to handle any length of string. Stop processing as soon as your comparison fails.
    Decide whether two adjacent letters which are identical trigger failure. And determine if the test should be case sensitive
    or not. The character 'Z' is numerically less than 'a' or any other lower case letter.

    Last edited by jim829; 10-18-2013 at 05:19 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 7
    Last Post: 10-11-2013, 12:34 AM
  2. ascending order or not in ascending order
    By hawkesey in forum New To Java
    Replies: 7
    Last Post: 01-16-2013, 12:56 PM
  3. Replies: 8
    Last Post: 11-23-2012, 04:39 PM
  4. Help with sort ascending and descending
    By Freeze69 in forum New To Java
    Replies: 6
    Last Post: 08-16-2011, 06:26 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