• 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

Alphabet question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is there an easier way to write a program that prompts the user for a character in the range of a-z (inclusive) and 3 integers in the range of 1-9 (inclusive). The program should convert the character into lowercase and get its numeric equivalent (where a is 1, b is 2 ... z is 26) and multiply the numeric equivalent by the sum of the integer inputs?

I was going to do something like int1=a, int2=b and so on so on but seems a little round winded no?

Thanks for the help,
Mario
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are confining yourself to letters in the range A-Z/a-z, it shou‍ld be easy to find the values for chars if you remember that a char is always implemented as a number. Look at the ASCII table and remember that the values are shown in hex with three digits across the top and the last digit on the left. There is a simple arithmetical way to get a=1 b=2 z=26 out of that table.
 
Mario Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lads - thanks for advice I went a different route but I'm stuck!!

At the moment the output is:
a123i

so if I enter "a" the output should be
a1235 a as we entered it, 1,2,3 as the integers entered and as
multiplication of the ints... but that's where I'm stuck..

If you see "String_5" I'm trying to basically multiple the output
of string_4 but aint happening... any ideas?


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have added code tags to your post. Always use the tags: doesn't it look better
What you are showing doesn't look the same as what you said before.
If you are using a Scanner, avoid nextLine(). Use nextInt for the three numbers, but I thought you were entering a single String "a123i". I can't understand how you turned that into "a1235". I thought you would be looking for the individual characters from the String, which is easy enough. There is no way to get an individual char with a Scanner, because it was designed to deal with “tokens” rather than characters. The nearest is something like myScanner.next().charAt(0), but that will cause problems if you have something more than one letter long, because it will discard most of your input.
I don't think your adding numbers to the text will work. You are right that you can subtract 'a' from your letter (well done noticing that), but it won't work for 'A'. But if you have something like 'u', you will end up adding "21" to the text, which I am sure you don't want. What is more, you won't be able to distinguish that 21 from 21 from "ab" or "21" in the original text.

Make life easy for yourself. Don't try to do everything at once. Don't do everything in the main method. Have one method for reading the String entered, one for getting the individual characters, maybe as a char[], one for creating an int[] from it, and one for doing the arithmetic. Write one method at a time and make sure it is working before you proceed to the next. You might be able to do the second and third things in one method. If you need to print an array for testing, this is probably the easiest way to do it:-
System.out.println(Arrays.toString(myArray));
You have to import java.util.Arrays.
 
Mario Jorge
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Campbell Ritchie - Thanks for the information and advice.

Will try explain the app better...

It will ask a letter between a-z - that letter is the assigned a number IE A=1, B=2 and so on.
Then your asked for 3 numbers... these numbers are then added together and multiplied by the numbered letter.
So if someone choose A its then 1 - they enter number 2, 3, 4....
the output will be A, 2, 3, 4, 9 (9 is 2+3+4= 9 - then 9X1(A) = 9)

Does that make sense?

Thanka a lot, Mario
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your number variables are actually Strings.  When you use the "+" operator on strings, it concatenates them.  That's why you get "1234" when you add the Strings "1", "2", "3", and "4". If you want to do arithmetic, you need actual numeric types. Scanner has a nextInt() method to return the user's input as an integer instead of a String.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what lines 37 - 44 are supposed to do, probably not what you intended.  Here's the code properly formatted:

This is why I say poor formatting is a bug.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So are you entering one four‑keystroke String or still four separate inputs? As I said yesterday, it is pretty easy to get the individual chars out of a String, charAt() probably being the simplest.
How do you get a1235 out of a123? 123 adds up to 6. It shou‍ld only take about 5 minutes to get to being able to print a 1 2 3 from an input of a123. Then you need to work out how to turn those chars into numbers. By the way: the (int) cast is unnecessary; that is done implicitly by the JVM when you try any arithmetic.

Don't call a String Char. Not only shou‍ld it start with a small letter (that prohibits the name because it becomes a keyword), but Char.charAt(0) is difficult to read, and Char doesn't tell you what the String means.
reply
    Bookmark Topic Watch Topic
  • New Topic