*****************READ PROBLEM BEFORE ANSWERING********************* I am tired o
ID: 3635108 • Letter: #
Question
*****************READ PROBLEM BEFORE ANSWERING*********************I am tired of people just pasting in programs they find on google without checking anything themselves.
This is my problem. My AuthenticateMain is unable to invoke the method isValid from Authenticate.
DO NOT ANSWER IF YOU DONT READ MY QUESTION
**********************************************************************************
import java.util.Scanner;
public class AuthenticateMain
{
public static void main(String[] args)
{
int[] actual_password = {6, 6, 5, 2, 0};
int[] random_nums = new int[10];
int[] entered_digits = new int[actual_password.length];
for (int i=0; i < 10; i++)
{
random_nums[i] = (int) (Math.random() * 3) + 1;
}
System.out.println("Welcome! To log in, enter the random digits from 1-3 that");
System.out.println("correspond to your PIN number.");
System.out.println();
System.out.println("PIN digit: 0 1 2 3 4 5 6 7 8 9");
System.out.print("Random #: ");
for (int i=0; i<10; i++)
{
System.out.print(random_nums[i] + " ");
}
System.out.println();
System.out.println();
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter code.");
int Index = 0;
for (int i=0; i<actual_password.length; i++)
{
String s = keyboard.next();
entered_digits[Index++] = s.charAt(0) - '0';
}
boolean isCorrect = isValid (actual_password, entered_digits, random_nums);
if (isCorrect)
{
System.out.println("Correct! You may now proceed.");
}
else
{
System.out.println("Error, invalid password entered.");
}
}
}
-------------------------------------------------------------------------------------------------
public class Authenticate
{
public Authenticate()
{
}
public boolean isValid(int[] actual, int[] entered, int[] randnums)
{
int Index = 0;
boolean Valid = true;
while (Valid && (Index < actual.length))
{
int Code = actual[Index];
if (entered [Index] != randnums [Code])
{
Valid = false;
}
Index++;
}
return Valid;
}
}
Explanation / Answer
In order to call a method from another class inside the static main you need to create a new instance of the class. I went ahead and did it for you.. The Authenticate class remains the same. The only difference is the AuthenticateMain class.. Hope this helps, please rate! import java.util.Scanner; public class AuthenticateMain { public static void main(String[] args) { Authenticate authenticate = new Authenticate(); int[] actual_password = {6, 6, 5, 2, 0}; int[] random_nums = new int[10]; int[] entered_digits = new int[actual_password.length]; for (int i=0; i < 10; i++) { random_nums[i] = (int) (Math.random() * 3) + 1; } System.out.println("Welcome! To log in, enter the random digits from 1-3 that"); System.out.println("correspond to your PIN number."); System.out.println(); System.out.println("PIN digit: 0 1 2 3 4 5 6 7 8 9"); System.out.print("Random #: "); for (int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.