Is there any way to get a map of java.awt.Point
s from a String? Or even just a single point on that String. For example for "xyz123\nabc123"
coordinate (0, 1)
would be 'a'
.
Asked
Active
Viewed 2,175 times
0

Hophat Abc
- 5,203
- 3
- 18
- 18
-
Maybe it's me but I don't understand your question. How does "xyz123\nabc123" indicate that (0,2) and (1,2) are y? – Mike Schachter Mar 18 '12 at 02:52
-
Sorry I worded that badly. I changed the question – Hophat Abc Mar 18 '12 at 02:56
3 Answers
0
It can be done but it's not a common representation so don't expect to find it done.
A String is not that different from a char array so these might help you:
0
There is nothing built-in for that.
You can try and parse that string into a 2D char array or a vector of char arrays (depending on if you know how many lines in total or not).
Given "xyz123\nabc123" as str
:
//split it by newline, should work on windows/unix
String lines[] = str.split("\\r?\\n");
char[][] map = new char[lines.length][];
//fill up map, each row is a new line
for(int i = 0; i < lines.length; i ++)
map[i] = lines[i].toCharArray();
//map[0][2] returns 'z'

XiaoChuan Yu
- 3,951
- 1
- 32
- 44
0
Not going to question your motive but here is one way:
String find(String[] a, int x, int y) {
try {
return String(a[x].charAt(y)); // or y+1 in your example
}
catch (Exception exc) {
return NULL;
}
}
...
String s = "xyz123\nabc546";
String[] sa = s.split("\\n");
find(sa, 0, 0); // returns "x"
find(sa, 1, 2); // returns "c"
find(sa, 3, 2); // returns NULL

Apprentice Queue
- 2,036
- 13
- 13