+ 2
Java - system security(not getting the loops right)
I am having issues with the following: import java.util.Scanner; class Demo{ public static void main(String[] args) { int[] passwords = {2021, 1023, 9929}; Scanner scanner = new Scanner(System.in); int password = scanner.nextInt(); /*iterate through the array “passwords” and compare items with inputted password.*/ if (password == passwords){ System.out.println("Welcome");} } } What am I doing wrong?
10 Antworten
+ 9
Saga Rúnarsdóttir ,
the code compares a single int value (from input) stored in `password` to a complete int[] array named `passwords` - which does not work.
> we need to loop through the array `passwords` and check if the input value matches to any one of the passwords stored in the array `passwords`. to do so, we can use a `for` loop.
+ 7
The problem feels like a lack of understanding on how For-each loop works.
in your example, you have:
for(int validPassword: password)
yet you still used password in your if statement:
if(inputPassword==(password)) ❌
password is an array.
inputPassword is an int.
you can't compare an int to an array.
The point of the for loop is that validPassword will represent an item of the password array for each iteration.
so what you should be doing is:
if(inputPassword == validPassword)✅
compare int to int.
+ 6
//the variable password holds the current value from the validpassword array
for (int password: validpassword) {
//Java is case-sensitive: "inputPassword" and "inputpassword" has different meaning
if (password == inputpassword) {
Good Luck
+ 5
Saga Rúnarsdóttir You Are getting closer and closer. Swap validpassword and password in the for-each loop, and also, there is no need to have password in parentheses in the if statement.
+ 5
Saga Rúnarsdóttir ,
as we can see, you have started to learn from the `introduction to java` tutorial. but we can not see how far you have progressed yet.
you can learn from this:
*introduction to java* tutorial
module *arrays*
looping over arrays
lesson arrays and loops:
(this code is provided from sololearn in the mentioned tutorial, and covers exactly what you need to learn => looping over the elements of an array)
public class Demo {
public static void main(String[] args) {
int[] ages = {18, 33, 24, 64, 45};
for(int x=0;x<ages.length;x++) {
System.out.println(ages[x]);
}
}
}
+ 1
I think I am just not getting the for if loop writing.
If not numerical.
How to loop if true seems beyond me 😭
Feels like I understand the previous matter on loops but not here
Tried this
import java.util.Scanner;
class Demo{
public static void main(String[] args) {
int[] passwords = {2021, 1023, 9929};
Scanner scanner = new Scanner(System.in);
int ok.password = scanner.nextInt();
/*iterate through the array “passwords” and
compare items with inputted password.*/
for(int ok.password:passwords){
if (password==passwords )
System.out.println("Welcome");}
}
0
Tried this as well did not work
import java.util.Scanner;
class Demo{
public static void main(String[] args) {
int[] validpassword = {2021, 1023, 9929};
Scanner scanner = new Scanner(System.in);
int inputpassword = scanner.nextInt();
/*iterate through the array “passwords” and
compare items with inputted password.*/
for (int validpassword : password) {
if (inputPassword == (password)) {
System.out.println("Welcome");}
}
}
0
You seem to be really close, though. There are a few little things to fix, and if fixed, it should work.
By the way, are you supposed to only take input or actually compare them to specific values?
0
Cc