0

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] = ???????????; } } } 

4 Answers 4

2

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; } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

clever using the if statement to do the 'conversion' to integers... Nicely done!
@zipzit There isn't an if statement there! Was that comment meant for me?
no, comment for Reut... My bad, not a pure if statement, he used the ternary operator to perform the if function. Thats not a tool most folks reach for first. Paul, I would have done it exactly the way you did it.
Thanks for the response, altough I get a nullPointerException I dont know why. I need to save it as an integer in order to compare it later.
@zipzit Fair enough :)
0

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); } } 

Comments

0

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++; } 

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.