Array “out of bounds” [duplicate]

和自甴很熟 提交于 2019-11-29 17:26:39

When you declare an array you provide the length/size of that array. An array starts at index 0 as you've done in your code, but you're trying to assign a value to an index "out of bounds" - the fourth index.

new String[3] // index 0,1,2
new String[4] // index 0,1,2,3

solution is very simple

String[] userInfo = new String[4];

You are using a smaller Array.... do this

String[] userInfo = new String[4];

I would suggest you to use Arraylist as this does not require any size to declare and can accomodate any number of items.

  ArrayList<String> userInfo = new ArrayList<>();
    userInfo.add(sourceTextField.getText());
    userInfo.add(usernameTextField.getText());
    userInfo.add(passwordTextField.getText());
    userInfo. dd(emailTextField.getText());
     for (String userInfo1 : userInfo) {
        try 
        {.......
           ............

And the rest of the code proccessed accordingly....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!