Question

Can someone fix the program below so it does what the picture says it won't work for me.

1-Write a subroutine that reads a word from the user. The subroutine gets the input from the keyboard and returns as output a

import java.util.Scanner;
public class Userpass {
static String arr[];
static int i = 0;
public static void main(String[] args) {
String username, password;
int tries = 0, result;
do {
System.out.print("Enter the username: ");
username = readUserInput();
System.out.print("Enter the password: ");
password = readUserInput();
result = verifyCredentials(username, password);
tries++;
if (result == -1)
System.out.println("The username is incorrect!\n");
else if (result == -2)
System.out.println("The password is incorrect!\n");
else if (result == -3)
System.out.println("The username and password are both incorrect!\n");
if (result == 1 || tries == 10) {
if (result == 1)
System.out.println("Successful login attempt!!\n");
else if (tries == 10)
{
System.out.println("You have exceeded the number of authorized login attempts.\n");
System.exit(0);
}
break;
}
} while (result != 1 || tries != 10);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the input sentence");
String line = sc.nextLine();
String[] words = splitFunction(line);
for(String word : words){
System.out.println(word);
}
}
private static String readUserInput() {
Scanner sc = new Scanner(System.in);
String inp = sc.nextLine().trim();
String res = "";
for (int i = 0; i < inp.length(); i++) {
if (i == inp.length() || inp.charAt(i) == ' ')
break;
else
res += inp.charAt(i);
}
return res.trim();
}
private static String[] splitFunction(String str) {
arr = str.split(" ");
return arr;
}
private static int verifyCredentials(String username, String password) {
if (!username.equals("admin")) {
if (password.equals("koolaid"))
return -1;
else
return -3;
} else if (username.equals("admin")) {
if (password.equals("koolaid"))
return 1;
else if (!password.equals("koolaid"))
return -2;
}
return 0;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The program is working for me. we can make a few improvements here and there. if you're facing any particular issue, please be elaborate.

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

//-----------------OUTPUT--------------------------------

Enter the username: abc Enter the password: koolaid The username is incorrect! Enter the username: admin Enter the password:

//-----------------------------------------------------

import java.util.Scanner;

public class Userpass {

    static String arr[];

    static int i = 0;

    public static void main(String[] args) {

        String username, password;

        int tries = 0, result;

        do {

            System.out.print("Enter the username: ");

            username = readUserInput();

            System.out.print("Enter the password: ");

            password = readUserInput();

            result = verifyCredentials(username, password);

            tries++;

            if (result == -1)

                System.out.println("The username is incorrect!\n");

            else if (result == -2)

                System.out.println("The password is incorrect!\n");

            else if (result == -3)

                System.out.println("The username and password are both incorrect!\n");

            if (result == 1 || tries == 10) {

                if (result == 1)

                    System.out.println("Successful login attempt!!\n");

                else if (tries == 10) {

                    System.out.println("You have exceeded the number of authorized login attempts.\n");

                    System.exit(0);

                }

                break;

            }

        } while (result != 1 || tries != 10);

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the input sentence");

        String line = sc.nextLine();

        String[] words = splitFunction(line);

        for (String word : words) {

            System.out.println(word);

        }

    }

    private static String readUserInput() {

        Scanner sc = new Scanner(System.in);

        String inp = sc.nextLine().trim();

        String res = "";

        for (int i = 0; i < inp.length(); i++) {

            if (i == inp.length() || inp.charAt(i) == ' ')

                break;

            else

                res += inp.charAt(i);

        }

        return res.trim();

    }

    private static String[] splitFunction(String str) {

        arr = str.split(" ");

        return arr;

    }

    private static int verifyCredentials(String username, String password) {

        if (!username.equals("admin")) {

            if (password.equals("koolaid"))

                return -1;

            else

                return -3;

        } else if (username.equals("admin")) {

            if (password.equals("koolaid"))

                return 1;

            else if (!password.equals("koolaid"))

                return -2;

        }

        return 0;

    }

}

//---------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Can someone fix the program below so it does what the picture says it won't work...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am creating a program that will allow users to sign in with a username and...

    I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such: Username Password I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a...

  • Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

    Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • Hashmap concept

    HashMap:  We can use a HashMap to store key value pairs.  Reminder that only reference types can be used as the key or value.  It you want to use a number as the key then an Integer data type could be used but not an int primitive data type.Create a class called Login which contains a HashMap as a private attribute.  It should also have an instance method called loadCredentials which accepts the two arrays.  It will load the HashMap...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • Debug and fix the Java console application that uses 2 dimensional arrays but the application does...

    Debug and fix the Java console application that uses 2 dimensional arrays but the application does not compile nor execute. Student enters only integers to select courses for registration from a menu. No data type validation of the user menu selection is checked or required. The program terminates only when the student closes it. No registration of other courses not displayed by the program . No registration more than once for the same course. No registration for more than 9...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT