0

say for instance i have a string ie

"my dog did a Foo"

i want to pass each character into an array list and then perform operations according to certain indexes.

so i need to convert the characters to their decimal values in this case :-

"my dog did a Foo" 

would translate to the following decimal representation:-

109 121 32 100 111 103 32 100 105 100 32 97 32 70 111 111

once this is done i need to send it to an array list and compile a string using the second number "121"

the last number "111" and another lets say "97" and will be doing calculations with those numbers.

so if these are parsed to an arraylist the index for the second number in this case "121" is index1, the index for "111" in this case would be the index equal to the strings length.(the last char) which means we need to determine this index before. and finally 97 which would be index 11.

how can i write a statement that will convert the string to decimal Char values, add each char to its own index in array list and then do calculations based on indexes? its been killing me for over a month!

the following code is terribly wrong but hopefully illustrates what i intend to do

if we use "i." to signify an index in the array.

k = 0;
private String lengthofstring = k;
    while (k <= lengthofstring){
System.out.println(i.1+i.11 + "-" + i.lengthofstring);

k++
}

supposedly printing :-

218-111(or whichever is at the index equal to lengthofstring)

any help would be amazing

many thanks in advance for any help u can offer.

2
  • 1
    Okay, you're asking several problems here. Two of them are essentially 'how do I use ArrayLists'. I recommend you simplify this down to ONE question and tell us what you've been doing all month to fix it. You can ask how other questions in their own topic at the same time too! Sound good? Commented Oct 27, 2014 at 17:29
  • You know that String class got charAt(int ind) method? Commented Oct 27, 2014 at 17:30

2 Answers 2

1

Having:

String inputString = "my dog did a Foo";

1. To convert a String to an array of chars:

In your case, maybe an ArrayList is not the best option. You could simply use a char array:

char[] allStringChars;

The String class already provides a method for that, or you can do this with some loop like the following algorithm:

declare allStringChars with the inputString size
declare i as 0
for each char on inputString do
    copy the char at inputString[i] to the allStringChars[i]
    increment i
end

To get a specific char from inputString, use inputString.chatAt(i) method.

2. Do operations on the values:

After to covert the inputString to a array of characters, your can obtain the integer value of any character:

int firstValue = allStringChars[0];
int lastValue = allStringChars[allStringChars.length - 1];

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Some Suggestions:

  1. you have a String so you can either covert to a array of type char one by one or use a ready function toCharArray() from String class.

  2. clearly, you need to define a dynamic array which shrinks and expands by itself.

  3. Since, char type is cast-able to int you can cast each elements one by one and add them to the list.

Since this is a homework so my code answer is :

In Java 8

Code:

   String s = "my dog did a Foo" ;
   List<String> listString = Arrays.asList(s.split(""));
   IntStream.range(0, listString.size())
           .map(i-> (int)listString.get(i).charAt(0))
           .forEach(i -> System.out.print(" " + i));

Output:

 109 121 32 100 111 103 32 100 105 100 32 97 32 70 111 111

6 Comments

I totally agree but It is quite clear how to solve this
I cannot argue with logical objection.
I'm certain that now your answer is much more valuable for a student willing to learn. I'm still not sure why dynamic array in your point 2.
Thanks. I know what dynamic array is, but it's a little abuse in that kind of problem. Since you get a non-volatile string s, you can allocate a static array int[] ints = new Int[s.length()].
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.