My task: read the two-dimensional byte array from the console. That he enrolled in the variable, and in the future I worked with him. I imagine it like this: the user enters the first line, presses Enter, the second, writes go and the array processing algorithm starts. Here is what I am trying:

  public static byte[][] input(int strok, int stolb){ Scanner sc=new Scanner(System.in); byte X[][]=new byte[strok][stolb]; int i,j; for (i=0; i<strok; i++){ for (j=0; j<stolb; j++){ X[i][j]=sc.nextByte(); } } return X; } public static void main(String[] args) throws Exception { Scanner sc=new Scanner(System.in); System.out.print("Введите количество строк: "); int strok=sc.nextInt(); System.out.print("Введите количество столбцов: "); int stolb=sc.nextInt(); Table pokr1=new Table(input(strok,stolb)); pokr1.putArr(); 
  • What is the question then? - Denis
  • how to implement it? How to handle this enter, so that it means moving to the next line? This code generally causes a Value out of range error when I try to enter something - Naddyson
  • and how the method call occurs? what's in main ? write Maine code - Alexey Shimansky
  • Uhh .. what about the Table? - Alexey Shimansky
  • This is a class that contains algorithms for converting this array. I can not figure out how to implement the input array, otherwise there are no problems - Naddyson

1 answer 1

The problem is that byte is a type with a range of possible values ​​from -128 to 127. As a result, if you enter a number less than -127 or more than 128, you will receive an error Value out of range

Most likely when entering numbers, you just specify an invalid value.

In this case, you can either change the data type to a larger one, for example, int and work with it.

Or control what the user entered, catching the error and inform the user that he must enter numbers in such a range.

  • That's it, the question is closed, thanks - Naddyson