-1

So i have an array of strings. Each index contains a string like "abc" "fnsb" "feros". I want to pass in this array of strings through a for loop that can get each character's ASCII value of ALL the strings listed above and possibly store each string's character's ASCII value in another array. For example, if my array of strings has

mystrings[0] = hi
mystrings[1] = hello
mystrings[2] = farewell

I want it to take the ASCII values of "h" and "i" and store it in an newarray[0], then take the ASCII values of "h","e","l","l","o" and store it into newarray[1], and ETC.

Note: Above is a bunch of pseudocode. Here is what I actually have:

String[] mystrings= new String[100];
double [] newarray = new double[100];
        for (int x=0; x<100; x++){
            char character = mystrings[x].charAt(x);
            int ascii = (int) character;
            newarray[x] = ascii;
            System.out.println(newarray[x]);
        }

Another note: There are indeed multiple strings stored in each index of the mystrings array. It's just in another part of my code that I don't want to share. So please assume that the "mystrings" array is properly filled with various strings. Thank you!

user7428413
  • 11
  • 1
  • 5
  • You want to store multiple ASCII values in a single variable of type `double`? How do you imagine that working? – Dawood ibn Kareem Jan 19 '17 at 03:15
  • How can you store multiple ASCII values in a single double? Just adding them all up? Some sort of encoding? As it stands it looks like you have the wrong data struct for `newarray` – John3136 Jan 19 '17 at 03:15
  • Your code makes no sense. Unless all the Strings in mystrings are 100 characters long it will quickly error out. And until that happens you will be storing the ASCII codes for "h", "e", and "r" into the first three elements of newarray. – Hot Licks Jan 19 '17 at 03:18
  • Possible duplicate of [Convert character to ASCII numeric value in java](http://stackoverflow.com/questions/16458564/convert-character-to-ascii-numeric-value-in-java) – Jobin Jan 19 '17 at 03:41
  • this looks more like an assignment given in school which you couldn't answer. – Mohammed Atif Jan 19 '17 at 03:46
  • @HotLicks i know it doesn't make any sense, that's how i know it's wrong. why else would i post it here? – user7428413 Jan 19 '17 at 03:54
  • @MohammedAtif thanks, but it's a smaller part of a huge program. but really thanks so much for your well needed contribution to my problem. useless. go troll somewhere else. – user7428413 Jan 19 '17 at 03:56
  • The problem is that you demonstrate a general lack of comprehension of what you're doing. We're not going to write your program for you, so you need to work on one misunderstanding at a time, rather than attempting something that is so far beyond your abilities. – Hot Licks Jan 19 '17 at 04:00
  • @user7428413, `but it's a smaller part of a huge program`, then you must explain the problem rather the approach. http://xyproblem.info/ . Go through this link, it will hardly take 10 minutes to read it. Based on that, ask your question again. And yea, unless you get trolled for your code, you cant improve your code. – Mohammed Atif Jan 19 '17 at 05:51

3 Answers3

1

The key issue in your pseudocode is that the result must be not simply an array, but an array of arrays:

int[][] newarray = new int[mystrings.length][];

The second issue is that getting character codes must be done in a separate loop, nested inside the first one. The loop must go from zero to mystring[i].length():

char character = mystrings[x].charAt(y);
//                                   ^

Note that charAt parameter is not the same as x in mystrings[x], because it needs to be a separate loop.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0
String s = "hello";
byte[] bytes = s.getBytes("US-ASCII");
Parviz Rozikov
  • 259
  • 2
  • 10
0

Here's a Java 8 solution:

String[] mystrings = {"hi", "bye"};

List<List<Integer>> result;

result = Arrays.stream(mystrings)
                 .map(s -> s.chars()
                                   .mapToObj(e -> (char) e)
                                   .collect(Collectors.toList()))
                 .map(chars -> chars.stream()
                                       .map(Integer::new)
                                       .collect(Collectors.toList())
                 )
                 .collect(Collectors.toList());

... and the output would be:

[[104, 105], [98, 121, 101]]
Aria Pahlavan
  • 1,338
  • 2
  • 11
  • 22