Question

In: Computer Science

How do I add a method to make sure that the user inputs in uppercase only...

How do I add a method to make sure that the user inputs in uppercase only and anything entered in lower case throws an error as well as to make sure when they are halving the questioned number they don't enter any decimals?

import java.util.*;

public class TestCode {
public static void main(String[] args) {
String choice = "YES";
Random random = new Random();
Scanner scanner = new Scanner(System.in);
ArrayList data = new ArrayList();
int count = 0,correct=0;
while (!choice.equals("NO")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println(randomInt);
System.out.println("Enter half of the random number");
int temp=scanner.nextInt();
if(2*temp==randomInt){
correct++;
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
data.add(randomInt);
count++;
System.out.print("Want another number (Yes / No)? ");
choice = scanner.next();
}
System.out.println("You got like "+correct+" answers and "+(count-correct)+" incorrect answers");
int min = Collections.min(data);
int max = Collections.max(data);

System.out.print("Values are: ");
for (int i = 0; i < data.size(); i++) {
System.out.print(data.get(i) + " ");
}
System.out.println();

System.out.println("Percentage of Yes values is " + (((count - 1) * 100.0) / count));
System.out.println("Maximum value is " + max);
System.out.println("Minimum value is " + min);
}
}

Solutions

Expert Solution

//Use this code

/**

You can use equalsIgnoreCase() method to allow user to choose option in any case. or If you want to only the UPPERCASE condition Please refer to solution (2)

*/

Solution (1):-->

import java.util.*;

public class TestCode {
    public static void main(String[] args) {
        String choice = "YES";
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        ArrayList data = new ArrayList();
        int count = 0,correct=0;
        while (!choice.equalsIgnoreCase("NO")) {
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println(randomInt);
            int temp=0;
            while (true) {
                System.out.println("Enter half of the random number");
                try {

                    temp = scanner.nextInt();
                    //Prevernt user to print only integer
                    if (((Integer) temp) instanceof Integer) {
                        break;

                    }
                    else
                    {
                        throw new InputMismatchException();
                    }
                }
               catch (InputMismatchException e)
               {
                   System.err.println("Please Enter integer numbers only..");
                   scanner.nextLine();
               }

            }

            if(2*temp==randomInt){
                correct++;
                System.out.println("Correct");
            }
            else{
                System.out.println("Incorrect");
            }
            data.add(randomInt);
            count++;
            System.out.print("Want another number (Yes / No)? ");
            choice = scanner.next();
        }
        System.out.println("You got like "+correct+" answers and "+(count-correct)+" incorrect answers");
        int min = (int) Collections.min(data);
        int max = (int) Collections.max(data);

        System.out.print("Values are: ");
        for (int i = 0; i < data.size(); i++) {
            System.out.print(data.get(i) + " ");
        }
        System.out.println();

        System.out.println("Percentage of Yes values is " + (((count - 1) * 100.0) / count));
        System.out.println("Maximum value is " + max);
        System.out.println("Minimum value is " + min);
    }

}

//output

//Solution(2)

import java.util.*;

public class TestCode {
    public static void main(String[] args) {
        String choice = "YES";
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        ArrayList data = new ArrayList();
        int count = 0,correct=0;
        while (!choice.equals("NO")) {
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println(randomInt);
            int temp=0;
            while (true) {
                System.out.println("Enter half of the random number");
                try {

                    temp = scanner.nextInt();
                    //Prevernt user to print only integer
                    if (((Integer) temp) instanceof Integer) {
                        break;

                    }
                    else
                    {
                        throw new InputMismatchException();
                    }
                }
               catch (InputMismatchException e)
               {
                   System.err.println("Please Enter integer numbers only..");
                   scanner.nextLine();
               }

            }

            if(2*temp==randomInt){
                correct++;
                System.out.println("Correct");
            }
            else{
                System.out.println("Incorrect");
            }
            data.add(randomInt);
            count++;
            System.out.print("Want another number (Yes / No)? ");
            choice = scanner.next();
            while (!validateInput(choice))
            {
                System.err.println("Please enter choice in UPPER CASE ONLY.");
                System.out.print("Want another number (Yes / No)? ");
                choice = scanner.next();
            }
        }
        System.out.println("You got like "+correct+" answers and "+(count-correct)+" incorrect answers");
        int min = (int) Collections.min(data);
        int max = (int) Collections.max(data);

        System.out.print("Values are: ");
        for (int i = 0; i < data.size(); i++) {
            System.out.print(data.get(i) + " ");
        }
        System.out.println();

        System.out.println("Percentage of Yes values is " + (((count - 1) * 100.0) / count));
        System.out.println("Maximum value is " + max);
        System.out.println("Minimum value is " + min);
    }
    private static boolean validateInput(String input)
    {
        //Convert to character Array
        char[] chars = input.toCharArray();
        for (int i = 0; i <chars.length ; i++) {
            if(Character.isLowerCase(chars[i]))
                return false;
        }
        return true;
    }
}

//output

//If you need any help regarding this solution .......... please leave a comment ........... thanks


Related Solutions

For a LinkedList in Java how do I - Add a method named replace() that takes...
For a LinkedList in Java how do I - Add a method named replace() that takes in two parameters (the data object to replaced, followed by the one to be inserted) that will remove the first occurrence of the item to be replaced and add the second parameter to the list. The method returns a boolean - true if the item to be replaced was found, false if not - Add an instance method named showList() (no parameters or return...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
Hello, please answer all 3 questions, if possible! :) I will make sure to add 5...
Hello, please answer all 3 questions, if possible! :) I will make sure to add 5 star rating! 29. Which of the following commands may be used to display boot error messages? (Choose all that apply.) a. dmesg | less b. less /var/log/boot c. less /var/log/wtmp d. less /var/log/boot.log 31. Which of the following can be used to obtain detailed performance statistics regarding virtual memory usage? (Choose all that apply.) a. Files stored within the /proc directory. b. The iostat...
How do I make sure that this C code shows the letter P for one second...
How do I make sure that this C code shows the letter P for one second then L a second later on a raspberry pi sense hat? How do I make sure that this C code shows the letter P for one second then L a second later on a raspberry pi sense hat? 1 #include <stdio.h> 2 #include <unistd.h> 3 #include "sense.h" 4 5 #define WHITE 0xFFFF 6 7 int main(void) { 8     // getFrameBuffer should only get called...
How do I use ASCPII that produces two alphabets where one is uppercase and the other...
How do I use ASCPII that produces two alphabets where one is uppercase and the other lowercase using C++ Lets use Y and/or A  
How do you validate in pseudocode a "REPEAT" "UNTIL" loop? For example if a user inputs...
How do you validate in pseudocode a "REPEAT" "UNTIL" loop? For example if a user inputs invalid data there should be an error message that reprompts for correct data. To show this can you write Pseudo Code for the following problem? Create an application that can determine the yearly bonus for all company employees. Not all employees are eligible for a yearly bonus. Only employees that have been with the company longer than 2 years and must have been given...
how do you make sure to evaluate and interpret equally
how do you make sure to evaluate and interpret equally
Q = Make quark model for spin 0 meson. attempt only if you are sure, i...
Q = Make quark model for spin 0 meson. attempt only if you are sure, i will downvote wrong or guessed answer.
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the...
Add the method getTelephoneNeighbor to the SmartPhone class. Make this method return a version of the phone number that's incremented. Given Files: public class Demo4 { public static void main(String[] args) { SmartPhone test1 = new SmartPhone("Bret", "1234567890"); SmartPhone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); SmartPhone test3 = new SmartPhone(); SmartPhone test4 = new SmartPhone("Carlos", "8189998999", "[email protected]"); SmartPhone test5 = new SmartPhone("Dan", "8182293899", "[email protected]"); System.out.print(test1); System.out.println("Telephone neighbor: " + test1.getTeleponeNeighbor()); System.out.println(); System.out.print(test2); System.out.println("Telephone neighbor: " + test2.getTeleponeNeighbor()); System.out.println(); System.out.print(test3); System.out.println("Telephone...
*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp********* *****PLEASE MAKE SURE...
*****NOTE: I DO NOT NEED A DRIVER FILE. I NEED mixed.h AND mixed.cpp********* *****PLEASE MAKE SURE THAT mixed.h AND mixed.cpp WORK WITH THE DRIVER FILE PROVIDED******************* Objective: Upon completion of this program, you should gain experience with overloading basic operators for use with a C++ class. The code for this assignment should be portable -- make sure you test with g++ on linprog.cs.fsu.edu before you submit. Task Create a class called Mixed. Objects of type Mixed will store and manage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT