Using the split method of the String class in Java

At the moment, I am studying Input-Output from books. The task is to get 2 numeric values from the keyboard input stream, which are separated by spaces when entering. After that, check whether they are integer values of the int type, use the parseInt method to convert them to int and send these values further to the program. There were no questions with the check. I decided to implement the input using the readLine method via the InputStreamReader packed in BufferedReader. There was no problem with that either. The question arose when splitting a string into substrings using the split () method by spaces. The idea was that a string divided into substrings is written to a string array and the first two elements of this array are passed to the method created for checking.

When I enter only one value without spaces, split has nothing to separate and it writes only one element of the array, as I understand it. When trying to output 2 values to the screen in a loop, it outputs the error of going out of the array boundaries. I created a variable with a reference to this array at the very beginning.

The actual question is: reasoning logically and reading the manuals. I came to the conclusion that the split() method creates its own array by the number of substrings and writes a reference to it to the variable I created earlier. So is this the case and how can this operation still be implemented?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
    static String[] buff = new String[2];
    public static void main(String[] args)
    {
        String edata;
        CheckInt checkInt;
        try(BufferedReader read = new BufferedReader(new InputStreamReader(System.in));)
        {
                edata = read.readLine();
                buff = edata.split(" ");
                for (int i=0; i<2; i++)
                {
                    System.out.println(buff[i]);
                }
        }
        catch(IOException exc)
        {
            System.out.print("Ошибка ввода вывода");
        }
    }
}

3 answers

Your loop consists of two iterations at i = 0 and i = 1. When an array consists of one element, it accesses the null element, and when the first element is accessed, an exception is thrown. Who prevents you from making a non-hard-skinned (fixed) restriction in the loop):

for (int i=0, end = buff.length; i<end; i++)

And enter at least a hundred values, even zero.

 0
Author: Евгений Глушков, 2018-06-07 12:44:30
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
    public final static int SIZE=2;
    static String[] buff;
    public static void main(String[] args)
    {
        String[] temp = new String[SIZE];
        String edata;
        CheckInt checkInt = new CheckInt();
        try(BufferedReader read = new BufferedReader(new InputStreamReader(System.in));)
        {
            System.out.print("Введите координаты: ");
            do
            {
                edata = read.readLine();
                buff = edata.split(" ");
                for (int i = 0; i < buff.length & i < temp.length; i++)
                {
                    temp[i] = buff[i];
                }
                System.out.println();
                for (int i = 0; i < temp.length; i++)
                {
                    System.out.println(checkInt.check(temp[i]));
                }
            }while(!(checkInt.check(temp[0]) & checkInt.check(temp[1])));
        }
        catch(IOException exc)
        {
            System.out.print("Ошибка ввода вывода");
        }
    }
}

So far, I could only come up with this, the question is whether it is possible to force split() to write to an already existing array, or does it create its own anyway?

 0
Author: Вячеслав Логиновский, 2018-06-07 12:55:20

And what prevents you from writing??

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class tet {
    public static void main(String[] args) {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    try {
        String edata = read.readLine();

        for(String temp : edata.split(" ")){
            System.out.println(temp);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

As a result, I got

2 5
2
5
 0
Author: Aidar Zainutdinov, 2018-06-08 07:08:20