Skip to content

Instantly share code, notes, and snippets.

@random-person-001
Last active May 20, 2016 15:14
Show Gist options
  • Save random-person-001/f05b5eb93d5a8e71dfc99dd75d3df6dd to your computer and use it in GitHub Desktop.
Save random-person-001/f05b5eb93d5a8e71dfc99dd75d3df6dd to your computer and use it in GitHub Desktop.
/**
* Create a String[height][width] (r,c?) array out of an inputed string that
* is at least that big (specified by its dimentions with /n).
*
* @param input a string with '/n's
* @return
*/
public static String[][] strToArray(String input) {
int width = input.indexOf("\n");
int height = occurrancesOf(input, "\n");//input.length() / (height);
System.out.println(height);
System.out.println(width);
char[][] charresult = new char[height][width];
input = "\n" + input; //So it works.
for (int i = 0; i < height; i++) {
input = input.substring(1 + input.indexOf("\n"));
System.out.println(input.substring(0, width));
charresult[i] = input.substring(0, width).toCharArray();
}
System.out.println();
System.out.println();
System.out.println(charresult[0]);
System.out.println(charresult[1]);
System.out.println(charresult[2]);
System.out.println(charresult.length);
System.out.println(charresult[1].length);
System.out.println("Convert char[][] to String[][]");
String[][] result = new String[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
result[i][j] = Character.toString(charresult[i][j]);
if (result[i][j].equals(null)){ //Use a ternary operator instead?
result[i][j] = " ";
}
}
//System.out.println(i);
}
return result;
}
String s
= "**** 1*** hihihi*** **** **** **** e*** **** ****e\n"
+ " ****2**** ****i***21234* **** **** **** **** ****\n"
+ "* ***3 **** ***i **** **** **** **** **** **** ***\n"
+ "** **4* **** **i* **** **** **** **tditiu* **** **\n"
+ "*** *5** **** **** **** **** **** **** **** **** *\n"
+ "**** 6*** **** **** **** **** **** **** **** **** \n"
+ " ****7**** **** **** **** **** **** **** **** ****\n"
+ "* ***8 **** **** **** **** **** **** **** **** ***\n"
+ "** **9* **** **** **** **** **** **** **** **** **\n"
+ "*** **** **** **** **** **** **** **** **** **** *\n"
+ "**** **** **** **** **** **** **** **** **** **** \n"
+ " **** **** **** **** **** **** **** **** **** ****\n"
+ "* **** **** **** **** **** **** **** **** **** ***\n"
+ "** **** **** **** **** **** **** **** **** **** **\n"
+ "*** **** **** **** **** **** **** **** **** **** *\n"
+ "**** **** **** **** **** **** **** **** **** **** \n"
+ " **** **** **** **** **** **** **** **** **** ****\n"
+ "* **** **** **** **** **** **** **** **** **** ***\n"
+ "** **** **** **** **** **** **** **** **** **** **\n"
+ "*** **** **** **** **** **** **** **** **** **** *\n"
+ "**** **** **** **** **** **** **** **** **** **** \n"
+ " **** **** **** **** **** **** **** **** **** ****\n"
+ "* **** **** **** **** **** **** **** **** **** ***\n"
+ "** **** 7*** **** **** **** **** **** **** **** **\n"
+ "*** ****6**** **** **** **** **** **** **** **** *\n"
+ "**** ***5 **** **** **** **** **** **** **** **** \n"
+ " **** **4* **** **** **** **** **** **** **** ****\n"
+ "* **** *3** **** **** **** **** **** **** **** ***\n"
+ "** **** 2*** **** **** **** **** **** **** **** **\n"
+ "** **** 1*** **** **** **** **** **** **** **** **\n"
+ "*** ****0**** **** **** **** **** **** **** **** *\n";
String[][] res = strToArray(s);
System.out.println(Arrays.deepToString(res));
for (String[] i : res) {
for (String j : i) {
System.out.print(j);
}
System.out.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment