1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java Exercise: Abecederian Program

Discussion in 'Java' started by hanleyhansen, Mar 1, 2009.

  1. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    I need to write a program in java that checks whether or not a word is an abecedarian. An abecedarian is a word whose letters appear in alphabetical order. Here is the exercise straight from the book:

    Exercise 7.6 A word is said to be "abecedarian" if the letters in the word appear
    in alphabetical order. For example, the following are all 6-letter English abecedarian
    words.
    abdest, acknow, acorsy, adempt, adipsy, agnosy, behint, beknow,
    bijoux, biopsy, cestuy, chintz, dehors, dehort, deinos, diluvy,
    dimpsy
    a. Describe an algorithm for checking whether a give word (String) is abecedarian,
    assuming that the word contains only lower-case letters. Your algorithm can be
    iterative or recursive.​
    b. Implement your algorithm in a method called isAbecedarian.

    Here is what I got so far:

    Code:
    public class Main {
        
        public static void main(String[] args) {
           String word1 = "abcdefg";
           int index = 0;
           char x = word1.charAt(index);
           char y = word1.charAt(index++);
           int z = x.compareTo(y);
           if (z < 0) {
            System.out.println ("name1 comes before name2.");
         } else if (z > 0) {
            System.out.println ("name2 comes before name1.");
         }
        }
        
    }
    The problem I'm having is in the part I bolded out. The error I get is "char can not be dereferenced". Does anyone know how I can fix this or how I can do this exercise anny other way? I've been working on it for hours be at the moment I'm pretty much stuck so any suggestions are greatly appreciated.
     
  2. hanleyhansen

    hanleyhansen New Member

    Joined:
    Jan 24, 2008
    Messages:
    336
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Drupal Developer/LAMP Developer
    Location:
    Clifton
    Home Page:
    Problem solved. If anyone is interested in my final code PM me and I'll send it to you.
     
  3. Marimuthu

    Marimuthu New Member

    Joined:
    Feb 29, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    The above code doesn't work since we can't use char data on compareto!!!!!!!!!!!

    Here is the final version

    :):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):):)


    Code:
    class abecedarian
    {
    public static  void main(String args[])
    {
    System.out.println(test("agnosy"));
    }
    public static boolean test(String s)
    {
    int x=s.length();
    int i=0;
    int j=1;
    int k=2;
    while (k<=x)
    {
    String a=s.substring(i,j);
    String b=s.substring(j,k);
    int flag=a.compareTo(b);
    if(flag<=0)
    {
    i++;
    j++;
    k++;
    }
    else
    {
    return false ;
    }
    }
    return true;
    }
    }
     

Share This Page