# Need help with arrays

John Roddick

Greenhorn

Posts: 3

posted 6 years ago

- 0

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!

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!

posted 6 years ago

- 0

Hi John,

Once you have scanned the input.

The string can than be converted into a charArray using the method provided by the String class.

Then you can take each element of the char array and obtain the single characters and apply your encryption logic to it.

I assume that you know how to access array elements or you need to read that.

Best of luck!

Once you have scanned the input.

The string can than be converted into a charArray using the method provided by the String class.

Then you can take each element of the char array and obtain the single characters and apply your encryption logic to it.

I assume that you know how to access array elements or you need to read that.

Best of luck!

Priety.

Campbell Ritchie

Sheriff

Posts: 47293

52

posted 6 years ago

- 0

Welcome to JavaRanch

Lots of people get the impression that a

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.

Lots of people get the impression that a

`char`s store characters. They don't. They are " . . . integers in the following ranges: . . . " You can do arithmetic with`char`s. . . . and you find out that the subtraction gives you results you can actually understandWhat 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.

posted 6 years ago

- 0

John, please use a meaningful subject line.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

John Roddick

Greenhorn

Posts: 3

posted 6 years ago

- 0

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 :

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.

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 .

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 :

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.

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 .

posted 6 years ago

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.

Henry

- 0

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.

Henry

Campbell Ritchie

Sheriff

Posts: 47293

52

John Roddick

Greenhorn

Posts: 3

posted 6 years ago

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~!

- 0

Campbell Ritchie wrote:Try printing thechars with arithmetic.Then you can see thatchars 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~!

I agree. Here's the link: http://aspose.com/file-tools |