+ 2

Questions on Arrays in Java

I'm trying to make sort of a login area. Where if you input a username that's not listed in the array, it creates a new temp array with one more capacity, imports all of the username's array into the temp array, plus the user's inputed username into it, then copies that array to my usernames array to add the new user's username to the list of usernames and same for the passwords then. That way if a user is already created, you need the pass to get in. If the user isn't created, it prompts to make a new account. The first code i'm linking is just the basic user and pass part. It links to my myClass.java file if successful user/pass. which is reaper3 for user and Dustin for pass. The second code i'm linking (password protection 2) is with the full code not commented so it tests adding input to arrays. I keep getting errors though with the parts that's adding the inputted username and passwords to the arrays. Please help me out? Password Protection 1 = https://code.sololearn.com/c4yl84bVBOx1/#java Password Protection 2 = https://code.sololearn.com/cx4eCiJ5fBj6/#java

5th Jan 2019, 4:46 AM
Dustin James Locey
Dustin James Locey - avatar
19 Answers
+ 3
x = test.length; z = x + 1; String[] tempuser = new String[z]; for(int i = 0, i < x, i++) { tempuser[i] = test[i]; test = tempuser; } test[z] = i.nextLine(); // 🔸In for loop you are assigning tempuser to test every iteration - you copied successfully only first index. This operation should be done after for. 🔸test[z] - here occurs index out of bound exception, change it to test[z-1]. I hope you already know why. // x = passwords.length; z = x + 1; String[] temppass = new String[z]; for(int i = 0, i < x, i++) { temppass[i] = passwords[i]; passwords = temppass; passwords[z] = confirmpass; } 🔸Same thing here, line passwords = temppass; after loop 🔸passwords[z] = confirmpass; also here change z to z-1 and out of loop // I didn't check code in compiler so there might be another error.
5th Jan 2019, 7:49 AM
Michal
Michal - avatar
+ 3
CheckIfUsernameExists - static method - static because you are going to use it in another static method. In static method you can only use static method or by creating new object and then call method - no so important here. It takes two parameters, string array and String 🔸Array - array with usernames 🔸String - given username // Inside method 🔸For - foreach - it iterates whole collection. Here you don't need index so I used this loop. Equivalent to: for(int i=0;i<array.length;i++) If any object from array is equal to given String return true otherwise false. //// resizeArrayAndAdd - this method is used for adding passwords and users, depends on passed array, string Takes two parameters 🔸 String array - usernames / passwords 🔸 value - username / password Create new, bigger array For loop copy data from old to new array Add new value then return new array. // Message limit 😄
5th Jan 2019, 6:52 PM
Michal
Michal - avatar
+ 2
Ok this one is working https://code.sololearn.com/cxMbihQ27cD5/?ref=app Problem was with your for loop You used "," instead of ";" as a separator And also you also couldn't initialize int i inside for because you already named scanner as "i". // edit I refactored your code a little bit https://code.sololearn.com/c0DIBfT05RoB/?ref=app
5th Jan 2019, 10:48 AM
Michal
Michal - avatar
+ 2
Dustin James Locey, I simply added two method: 🔸 checkIfUsernameExists - this one is searching for given username in array, in your example you hard coded first two indexes, this one is universal. 🔸 resizeArrayAndAdd - method resize array and add given value at the end then returns new array. I don't know why you wanted to use arrays but work with lists is much easier. I think you can practice and refactor code but this time with lists?
5th Jan 2019, 6:30 PM
Michal
Michal - avatar
+ 2
Dustin James Locey oh, ok then happy learning 😊.
5th Jan 2019, 6:34 PM
Michal
Michal - avatar
+ 2
when you have b<=test.length statement and array with 2 elements you are iterating from 0 to 2 but there is no such index (2)
5th Jan 2019, 9:13 PM
Michal
Michal - avatar
+ 1
Also, I used BlueJ to run this. so I'm not sure how my inputs are going to work with code playground
5th Jan 2019, 4:52 AM
Dustin James Locey
Dustin James Locey - avatar
+ 1
I just changed everything you said, and it's still giving me 51 errors through BlueJ. I'm getting errors in Password Protection 2 code. On lines 33-35, 48-49, 53, 64, 66, 68-89, 91-94, 97, 99, 101, And 103. I don't understand what I have wrong though :/
5th Jan 2019, 9:04 AM
Dustin James Locey
Dustin James Locey - avatar
+ 1
Dustin James Locey I will check it when I will be at home
5th Jan 2019, 9:06 AM
Michal
Michal - avatar
+ 1
I feel kinda dumb, but that all makes sense lol. Can you maybe add some detail explaining some of the things you refactored? I see you changed how it looks is the username is stored already. Could you explain some of that to me? Lol
5th Jan 2019, 6:23 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
I hadn't learned about lists yet. I'm in the java tutorial, in classes and objects. I just learned about methods and multidimensional arrays
5th Jan 2019, 6:32 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
Thank you. Can you maybe go into a little detail about what every piece of these 2 methods do and how they work with my code by chance? Some of what's new here I havent learned. I learned how to make a method and to call it. But you added parameters I believe it's called when you called the methods. These are what I'm confused on: private static boolean checkIfUsernameExists(String username, String[] users) { for (String user : users) { if (user.equals(username)) { return true; } } return false; } static String[] resizeArrayAndAdd(String[] array, String value) { String[] tempArray = new String[array.length + 1]; for (int i = 0; i < array.length; i++) { tempArray[i] = array[i]; } tempArray[tempArray.length - 1] = value; return tempArray; } }
5th Jan 2019, 6:37 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
So..I removed a lot of what i had that was un needed. And kept my design so far just because that's what I've learned to this point. But changed it like you said. I tried to make a new account, as you can see in the pic I'm going to try to attach, but I have my else statement if the user and pass aren't in the array to say wrong pass. And it outputs wrong pass for the capacity of my array. Which I domt want. And I also get an outofboundsexception error in BlueJ. Also shown in pic if I can attach one. https://code.sololearn.com/cx4eCiJ5fBj6/?ref=app The pic is here: https://ibb.co/2P7w9Dq
5th Jan 2019, 8:48 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
For(int b = 0; b<test.length;b++) You have b<=test.length and it produces indexoutofbound exception
5th Jan 2019, 9:01 PM
Michal
Michal - avatar
+ 1
So should I make an int variable equal to test.length? Then input the variable in the for statement?
5th Jan 2019, 9:03 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
Just change from b<=test.length to b<test.length
5th Jan 2019, 9:09 PM
Michal
Michal - avatar
+ 1
Oh, you cant have a <= in a for statement? Or just not with test.length following it?
5th Jan 2019, 9:11 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
Ohhh, because it starts from 0
5th Jan 2019, 9:13 PM
Dustin James Locey
Dustin James Locey - avatar
+ 1
I used your code instead of mine, where you changed a few things. And it works and all. And adds the user and pass to the array. But when the code is stopped, the array's inside the original .java file stay the same. It doesn't actually add that user and pass into the array for future use when the program is started back up again
5th Jan 2019, 11:50 PM
Dustin James Locey
Dustin James Locey - avatar