In: Computer Science
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);
}
}
//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