我正在制作一个简单的密码库,该密码库是一个二维数组[20] [3]。我可以输入一行然后将其放入数组,但是当我输入第二个条目时,它将覆盖上一个条目。
我尝试添加第二个for循环,因此它将在添加新条目后下降到下一行。
import java.util.Arrays;
import java.util.Scanner;
public class passSimple
{
static char[][] conversionTable = new char[27][2];
static String[][] passVault = new String[20][3];
static String password = "";
static String username = "";
static String website = "";
public static void main(String[] args)
{
getMorePass();
}
public static void getMorePass()
{
Scanner input = new Scanner(System.in);
System.out.println("What website is this for? (No space and all capitals): ");
website = input.next();
System.out.println("Whats your username? (No space and all capitals): ");
username = input.next();
System.out.println("Whats your password? (No space and all capitals): ");
password = input.next();
System.out.println("Would you like to add another password? ");
String check = input.next();
AddPassword(password, username, website);
System.out.println();
if(check.compareTo("YES") == 0)
{
getMorePass();
}
else;
}
public static void addPassword(String password, String website, String username)
{
//SavePassword
for (int x = 0; x < passVault.length; x++)
{
for (int y = 0; y < passVault.length;y++)
{
passVault[x][y] = username;
passVault[x][y] = website;
passVault[x][y] = password;
}
}
System.out.println(Arrays.toString(passVault[1]));
}
}
我希望输出为“
[website2, username2, password2]
[website1, username1, password1]
但是它用[website1, username1, password1]
代替了[website2, username2, password2]
答案 0 :(得分:0)
我建议你
public static void addPassword(String password, String website, String username){
for (int x = 0; x < passVault.length; x++){
if(passVault[x][0]==null && passVault[x][0]==null && passVault[x][0]==null){
//put only if there are no value
passVault[x][0] = username;
passVault[x][1] = website;
passVault[x][2] = password;
break;
}
}
System.out.println(Arrays.toString(passVault[1]));
}
但是使用List可能会更简单