• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abecedarian Testing - Problem with char's and While loops

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This in an exercise in my 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, befist, behint, beknow,
bijoux, biopsy, cestuy, chintz, deflux, dehors, dehort, deinos, diluvy,
dimpsy

What this excercise wants us to do is simply test if a string is abecedarian or not. Here is my code so far:



... The output of this code is:

'Acknow' is an abecedarian?: true
'Sevenfold' is an abecedarian?: false
'Abcdefgh' is an abcedarian?: true
'Abcdefhg' is an abcedarian?: true

______________________________
What I noticed is that my while loop seems to only be testing for the first 2 letters of the string. Hence, it realizes that "sevenfold" is not abecedarian, but doesn't realize that "abcdefhg" is not either. What did I do wrong/what should I change in my program so that it tests ALL the letters in each string?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You never change the values of a and b in your loop, so all you are doing is continuously comparing the first and second letters.

You've also got way too many variables in there. Keep it simple and get rid of x (replace it with index), first and second (just use s.charAt()), a and b (you can compare chars using >, so you don't need to convert them to ints) and length (replace it with s.length()).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic