Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Converting String Letters to Numbers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am new to Java programming and I am having a hard time on converting letters to numbers, what I want my program to happen is that for example a person types a string "I am Happy" then the output would be numbers, but in reverse for example "25-16-16-1-8013-109" include the - from the output, also the output is like that because the letters are suppose to have corresponding values example A = 1 B = 2 and so on. I = 9, space =0, A = 1, M = 13, space = 0, H = 8, A = 1, P = 16, P = 16, Y = 25. I think I almost got the code, but for some reason new problems occur and that is A and a should have the same values 1 uppercase and lowercase also how do I add the - and make sure that whenever there is a space the - would not occur same as 109 and 8013. Anyways here is the code I hope someone can help me and at the least give me advices and tips, we are allowed to have 1 main method and 2 user defined methods.


 
Marshal
Posts: 71752
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for using the code button, but you didn't quite get it right; the [/code] part goes after the code, but I have corrected it, and doesn&aops;t it look better now

You aren't far off.
Make the names of your code reflect what they do; New3 will mean nothing to you after next week
What does line 13 mean? I think you can delete that line without noticing anything.
Look at this loop, which is the standard way to traverse an array backwards:-Yes, you do need the slightly awkward syntax but that loop will definitely work for arrays of any size.
Find out what happens if you print escape characters:The subtraction will work, but consider using the % remainder operator instead of subtraction. I shall let you work out what to do. I can make that technique work for letters in English, but will fail to allocate numbers correctly for German, French, Spanish, etc.
Please indent your code correctly; lines 24‑27 are a good example of how not to do it, I am afraid. Always use {} after if.
 
reply
    Bookmark Topic Watch Topic
  • New Topic