Question
You can use the validation methods in this file from other code files in this project: String name = Validation.getString(in, Enter your name, 20); int
You can use the validation methods in this file from other code files in this project:
String name = Validation.getString(in, Enter your name, 20); int age = Validation.getInteger(in, Enter your age, 0, 80);
But there are several errors in the Validation.java file. Find them and fix them.
(This "validation" method is going to be called to ultimately write a histogram, but there are several errors that need to be fixed before it works properly. The desired histogram output is included at the end.)
/** * */ import java.util.Scanner; /** * @author * */ public class Validation { public static String getString(Scanner in, String prompt, int maxLength) { boolean valid = true; String s = ""; System.out.print(prompt); System.out.print(" : "); do { s = in.nextLine(); if (s.length() > 0) // ignore blank lines { if (s.length() < maxLength) { valid = true; } else { System.out.printf("Minimum length is %d characters. Try again."); } } } while(!valid); return s; } public static int GetInteger(Scanner in, String prompt, int lower, int upper) { boolean valid = false, outOfRange = false; int input = -1; do { System.out.print(prompt); if (outOfRange) System.out.printf(" [between %d and %d inclusive]", lower, upper); System.out.print(": "); while (in.hasNextInt()) { in.nextDouble(); System.out.println("Please enter an integer."); } input = in.nextInt(); if (input >= 0 && input <= 100) { valid = true; } else { System.out.println("Value is out of range."); outOfRange = true; } } while (!valid); return input; } }
Catch non-integers (doubles), strings and values out of range
Histogram Generator by Student
Enter the histogram title : Favorite Languages of Students
Enter the number of items: 100
Value is out of range.
Enter the number of items [between 0 and 10 inclusive]: x
Please enter an integer.
4
Enter item 1 : Java
Enter value 1: 10
Enter item 2 : Supersized Java
Maximum length is 10 characters. Try again.
Super Java
Enter value 2: 5
Enter item 3 : C++
Enter value 3: 7
Enter item 4 : JavaScript
Enter value 4: 50.0
Please enter an integer.
50
Value is out of range.
Enter value 4 [between 0 and 25 inclusive]: 3
Favorite Languages of Students
Java @@@@@@@@@@
Super Java @@@@@
C++ @@@@@@@
JavaScript @@@
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started