Hey all, new here and extremely new to Java as well. Spent some days covering some of the basics and played around with some sample basic beginner codes available on the net in order to get a better grasp on Java. This might be a sudden big jump for me, but I wanted to get my program to read the string input by a user, take each letter of the string and convert it into an integer via a mathematical formula I have in mind. It's more or less an encryption of some sort.

Unfortunately I'm quite stuck there. I know that I'll be using the scanner class for the user input, but how exactly do I get the program to convert EACH letter of the input string? For instance, let's say the user input the string "BALL." I'd like to convert the string into an integer by taking the predefined value of each letter of the string and multiply each one by 20, and add them all together.

Speaking of predefined values, I'm thinking of just doing :

int (letter of the alphabets from A to Z) = (a particular value for each letter of the alphabets from A to Z).

Am I on the right track? So in the example above, assuming I have a predefined values of B = 78, A = 46, L = 44, then the result integer for the string "BALL" would be : 20 (78 + 46 + 44) = 3360

I read once about charArray - I didn't quite get the concept but it did sound relevant to this problem I'm having, would anyone enlighten me on this please? I think I'll be fine with the rest of the codes, and hopefully I'd be able to utilise the newly learned IF statements as well so that the program would return an error should the number of characters in the string exceed a particular value. I just need to know how to get past my first problem. I'd really appreciate it if someone would be able to just guide me(just a glimpse of the solution or some hints would be more than enough) so I'd be able to get past this stage and learn from it and understand it completely. Thank you in advance!

Lots of people get the impression that a chars store characters. They don't. They are " . . . integers in the following ranges: . . . " You can do arithmetic with chars. . . . and you find out that the subtraction gives you results you can actually understand

What you are suggesting is possible; it isn't encryption or coding however, because you can't get back from 3360 to BALL. You would need a Map to store the before and after values (that is an excellent example of Map use).

I suspect your project might be too difficult for a real beginner.

Thanks guys for the replies - and thanks for noticing me of the inappropriate topic, I've changed it promptly.

Anyway, I've got as far as this :

testString = new String(input.next());

char testArray[] = testString.toCharArray();

for (int i=0; i<testArray.length; i++)

System.out.print(array[i]);

So then, from what I understood of my codes so far, I've basically stored the input string into an array called 'testArray.' Then the program reads each element of the string starting from the very first one(position 0), terminating the loop once all the elements of the string have been read which is when the length of my string = i. It then prints out whatever it is stored in the array. Did I get the concept right?

And I'm still stuck with the conversion or the one way encryption. How exactly do I define the values for each letter of the alphabets? And how do I go about referring them in the codes? I'm thinking that the mathematical operations must go here :

for (int i=0; i<testArray.length; i++) { Mathematical Operations Here }

So in case where the input is BIRD for instance, I should have done :

I was thinking of { 20*(testArray[0] + testArray[1] + so forth) } but it didn't seem to work.

Please guide me on how do I set up the predefined values and how to make reference to them in the mathematical operations. I'd really appreciate any input on this, thanks .

John Roddick wrote:
for (int i=0; i<testArray.length; i++) { 20*(Bvalue + Ivalue + Rvalue + Dvalue) }

I was thinking of { 20*(testArray[0] + testArray[1] + so forth) } but it didn't seem to work.

First, you may want to review using loops -- if you are going to hardwire the mathematical expression, to the array elements (so forth) directly, then you don't really need a loop.

Second, you need to elaborate what you mean by "didn't seem to work". It isn't very clear.

Try printing the chars with arithmetic.Then you can see that chars really are numbers.

John Roddick
Greenhorn

Joined: Oct 20, 2009
Posts: 3

posted

0

Campbell Ritchie wrote:Try printing the chars with arithmetic.Then you can see that chars really are numbers.

That works wonders, thank you! Subsequently, I was able to write a program to convert an input integer(regardless of number of digits) into a 5 letter string using the same method. Finally beginning to understand the concept of arrays. Thanks guys for all the input, couldn't have done it without your help~!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43268

32

posted

0

Well done

Don't get me started about those stupid light bulbs.