Reputation: 1
I have a properties file that contains this information:
info.row1=1100011
info.row2=1000001
info.row3=0001000
info.row4=0011100
info.row5=0001000
info.row6=1000001
info.row7=1100011
Also I have a matrix like this
info = new int[7][7];
I want to save each int number in a part of the matrix, like this:
---------------
|1|1|0|0|0|1|1|
---------------
|1|0|0|0|0|0|1|
---------------
|0|0|0|1|0|0|0|
---------------
|0|0|1|1|1|0|0|
---------------
|0|0|0|1|0|0|0|
---------------
|1|0|0|0|0|0|1|
---------------
|1|1|0|0|0|1|1|
---------------
How can I do that? I have this code that works well until now, I just need to save that info in the matrix.
private void startInfo(Properties data)
{
info = new int[7][7];
for(int i = 0; i < 7; i++)
{
for(int j = 0; j < 7; j++)
{
String estate = data.getProperty( "info.row" +i );
info[i][j] = ???????????;
}
}
}
Upvotes: 0
Views: 105
Reputation: 1
Thanks to all for the answer, you all resolved my doubt. Although, what no one realized (including me) is that my properties data started in 1, for example the first line was "info.row1", so when we started the first "for" in 0, we were searching for something that didnt exist, and a nullpointerexception error was caused. So the solution is to add 1 to i and problem solved. THANKS
Upvotes: 0
Reputation: 21995
Here is a solution using RegExs and a loop
Matcher m = Pattern.compile("=\\d*").matcher("info.row1=1100011 info.row2=1000001 info.row3=0001000 info.row4=0011100 info.row5=0001000 info.row6=1000001 <info.row7=1100011");
int[][] info = new int[7][7];
int counter = 0;
while (m.find()){
String s = m.group(0).substring(1);
for (int i = 0 ; i < s.length() ; i++){
info[counter][i] = s.charAt(i);
}
counter++;
}
Upvotes: 0
Reputation: 201497
You could use Character.digit(char, int)
to perform the int
conversion. Something like
info = new int[7][7];
for (int i = 0; i < info.length; i++) {
String estate = data.getProperty(String.format("info.row%d", i + 1));
char[] line = estate.toCharArray();
for (int j = 0; j < line.length; j++) {
info[i][j] = Character.digit(line[j], 10);
}
}
Upvotes: 0
Reputation: 31339
Read the data once per row and use the String.charAt
function to map to integers:
private void startInfo(Properties data) {
info = new int[7][7];
for (int i = 0; i < 7; i++) {
// only read once per row
String estate = data.getProperty( "info.row" +i );
for (int j = 0; j < 7; j++) {
// map '0' to 0, anything else to '1'
info[i][j] = estate.charAt(j) == '0' ? 0 : 1;
}
}
}
Upvotes: 2