Good day!

There is the following task shown in the picture: It is necessary to create an array of masks for each element entered from the keyboard. for character # = this is 1, for character. this is 0.

enter image description here

From the keyboard, we enter the following sequence:

Input Format: #...# ..... #.... ..... 

Next you need to get the array shown in the image.

The problem is that when you try to read a row, after reading the first row, we get an array

 [1,0,0,0,1] 

Further, when reading the second line, we should get an array

 [01,00,00,01] 

at the third reading

 [101,000,000,001] 

and at the fourth

 [0101,0000,0000,0001] 

But in reality, we do not add data to the cell of the array, but rewrite it every time we read the data.

  byte[] maskArray; Scanner sc = new Scanner(System.in); maskArray = new byte[5]; for(int j=0;j<4;j++){ String s; if(s.charAt(i) == '#') { byte num = 1; byte a = maskArray[i]; byte c = (byte) (a + num); maskArray[i] = c; }else if (s.charAt(i)=='.'){ System.out.print("0"); byte num = 0; byte a = maskArray[i]; byte c = (byte) (a + num); maskArray[i] = c; } } } 

I understand that I may be doing wrong, and this is most likely the case, but they set the task to sort out this issue, but there is no Java skills, only with javascript.

I would be grateful for the help.

Have a nice day, everyone!

  • Can you try to prepare a reproducible example ? In the given fragment a lot of unclear. Where does the result go? Where are the lines read? Why only one? Why only 4 characters? Why are the bits stacked up? - default locale
  • How to set the specified bit in the mask can be viewed in this question, for example: ru.stackoverflow.com/questions/281650/… - default locale

1 answer 1

If I understand you correctly, here is the working code.

 public class test { public static void main(String[] args) { byte[] maskArray; Scanner sc = new Scanner(System.in); maskArray = new byte[5]; for (int j = 0; j < 4; j++) { String s = sc.next().trim(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '#') maskArray[i] |= 1<<j; } } for (int i = 0; i < maskArray.length; i++) System.out.println(Integer.toString(maskArray[i], 2)); } 

}

if you need to build on the contrary, we replace it

 maskArray[i] = (byte) (maskArray[i] + Math.pow(2,j)); 

on this

 maskArray[i] = (byte) (maskArray[i]*2 + 1); 

At the expense of the job did not understand. What does it mean the values ​​do not specify and add.

using i ++ or something?

  • Thank you Works! There is only one problem. When we have an array cell starts at 0 - for example when entering: .. ##. We should get: 00110 And we get: 110. Ie the first zeros are removed. It seems to be logical, but causes a certain problem. The fact is that later we will have to use this mask for a bitwise comparison with another mask. Using the first character of our mask and the first character of another mask. In the case when zeros are removed - our first character will always be the first one. And we need to keep all zeros. How can I do that? - mDobroch
  • These are bits of a byte field. The first bit is the eighth bit from the end, which, when filling five bits, will always be zero. How do you find the first non-zero bit and why? If you need the first zeros to remain, then it would not be necessary to convert them to bytes and make the string. - IL Mare
  • I had an idea to make a string, but then the question arises how then to use the XOR (^) operation between two cells of arrays. The task is generally to enter the max. count squares 2x2 in the field indicated in the figure. It is impossible that the squares intersect. Moreover, the # sign is a busy place, it is impossible to enter into it. One of the stages of solving the problem is to obtain an array of the field mask (for further work with it using the & & ^ operators). Zeros are responsible for the empty square, units for busy. As far as I understand, getting a mask cell [0001] - we can then compare it with a cell by approx. [0101] and analyzed - MDobroch
  • An idea to use something of this kind appeared: System.out.println (String.format ("% 03d", Integer.toString (maskArray [i], 2))); but I get an error. If I try: System.out.println (String.format ("% 03d", maskArray [i]) - then everything is OK, - the zeros are added but in the cells the entry is not converted to the bit data format. - MDobroch
  • There is clearly something wrong. 1) if you need operations & | ^ you can do them with the help of simple logic on numbers. 2) if you need to store these values ​​as byte bits, then why do you translate them into a string. - IL Mare