0

I've seen some other questions with the same title, but it isn't the same problem. I'd like to convert strings that are contained in an array into arrays themselves.

So I've got this :

String str = "hello";

That I'm able to turn into a string array with this :

String[] arr = str.split("");

Which gives {"h", "e", "l", "l", "o"}

So I'd like to turn this into an array of objects in which every object would be a string array. So that I would be able to call h[0] (earlier initialize) in something like System.out.println(h[0]).

I assume this isn't pretty clear since I'm not a native English speaker, so I would be glad to sharpen my explanations.

EDIT : for example I initialize

String[] h = {"*   *",
              "*   *",
              "*****",
              "*   *",
              "*   *"};

and other "letters" before all of that, and eventually I want to print with

for (int l=0; l<str.lenght(); l++) {
    //Do what I explained above which would return the array letter = h (also the array)
    for (int s=0; s<5; s++) {
        System.out.println(letter[i]);
    }
}

And the output shows "hello" char of * by char of *.

N. Cornet
  • 121
  • 2
  • 8
  • 2
    Can you provide an exemple of one or two desired input and output for what you want to do? – emiliopedrollo Sep 29 '16 at 22:06
  • 1
    So are you just looking for a double array of strings, like `String[][] doubeArr;`? – nhouser9 Sep 29 '16 at 22:06
  • 1
    You can't actually do that in Java. (see http://stackoverflow.com/questions/8631935/creating-a-variable-name-using-a-string-value). – azurefrog Sep 29 '16 at 22:07
  • That's a tricky question, for what I understood you want to as result for a String "Hello" an array of Objects that contain an instance of an class named h int the first index, an instance of an class named e on the second index and so on. Is that right? – emiliopedrollo Sep 29 '16 at 22:17
  • Can it be a List instead of an Array? – emiliopedrollo Sep 29 '16 at 22:19
  • array= ( "String1", "String2", "String3" ) ?, or array( array("S","t","r","i","n","g","1"), array(...), array(...) )? do they have to be strings ayway? cant be char?, I ask because if `h` is an array of Objects, where every object is a string array, using h[0] would print an address (pointer, useless data to you), not a string. – gia Sep 29 '16 at 22:23
  • Hello. Since you are not very active/new Stack Overflow user you may not know that to update your question with new information (like example of what you want to achieve) you need to use [edit] option. I encourage you to use it since in current state your question is not very clear (at least for me). – Pshemo Sep 29 '16 at 23:02
  • @emiliopedrollo That might be what I want, but I'm not sure I understand fully your comment... Anyway I've updated the question to precise what I really want. – N. Cornet Sep 30 '16 at 06:37

2 Answers2

0

Im trying to read your mind here but:

char S[][] = new char[20][];

S[0] = "hello".toCharArray();
S[1] = "anotherstring".toCharArray();
...
S[19] = "".toCharArray();

System.out.println( S[0][0] ); //h
System.out.println( S[1][5] ); //e
System.out.println( S.length ); //20, we have room for 20 strings
System.out.println( S[19].length ); //0 because string S[19] is empty
System.out.println( String.valueOf(S[0]) ); //hello
System.out.println( S[19][0] ); //error because string is empty

Alternatively you can have a simple array of Strings (String[]) and access individual chars with the function charAt(i)

gia
  • 757
  • 5
  • 19
0

If what you want to do is turn this:

String = "hello";

Into this:

String[] h;
String[] e;
String[] l;
String[] l;
String[] o;

It can't be done. Java needs to know the name of the variables during compile time, you can't name a new variable based on some random input.

You can, however, define all possible letters and then return only the ones you need; something like this:

final String[] LETTER_A = {"  *  ", " * * ", "*****", "*   *", "*   *"};
final String[] LETTER_B = {"**** ", "*   *", "**** ", "*   *", "**** "};
// ...
final String[] LETTER_H = {"*   *", "*   *", "*****", "*   *", "*   *"};
// ...
final String[] LETTER_Y = {"*   *", " * * ", "  *  ", "  *  ", "  *  "};
final String[] LETTER_Z = {"*****", "   * ", "  *  ", " *   ", "*****"};

public void printASCII(String s) {
    for (String c : s.toUpperCase().split("")) {
        switch(c) {
            case "A":
                for (int i = 0; i < LETTER_A.length; i++) {
                    System.out.println(LETTER_A[i]);
                }
                System.out.println();
                break;
            case "B":
                for (int i = 0; i < LETTER_B.length; i++) {
                    System.out.println(LETTER_B[i]);
                }
                System.out.println();
                break;
            // ...
            case "H":
                for (int i = 0; i < LETTER_H.length; i++) {
                    System.out.println(LETTER_H[i]);
                }
                System.out.println();
                break;
            // ...
            case "Y":
                for (int i = 0; i < LETTER_Y.length; i++) {
                    System.out.println(LETTER_Y[i]);
                }
                System.out.println();
                break;
            case "Z":
                for (int i = 0; i < LETTER_Z.length; i++) {
                    System.out.println(LETTER_Z[i]);
                }
                System.out.println();
                break;
        }
    }
}

Output of printASCII("Yahbyz");:

*   *
 * * 
  *  
  *  
  *  

  *  
 * * 
*****
*   *
*   *

*   *
*   *
*****
*   *
*   *

**** 
*   *
**** 
*   *
**** 

*   *
 * * 
  *  
  *  
  *  

*****
   * 
  *  
 *   
*****
walen
  • 7,103
  • 2
  • 37
  • 58
  • Thank you I'll try this, but I'm quite new in Java, so do I have to write this code in `public static void main` or somewhere else ? – N. Cornet Sep 30 '16 at 08:57
  • That's up to you. Maybe you'd like to have a look at the Documentation section about [How to write a Java program](http://stackoverflow.com/documentation/java/84/java-overview) or google some Java tutorials. Also, if you think my answer above helped you, it'd be nice if you accepted it, too. – walen Sep 30 '16 at 11:10
  • Yes, I was about to do that, I just wanted to test it first. Thank you for your answer ! – N. Cornet Sep 30 '16 at 11:41