Java data validation with while loops - java

This applicaiton validates that the user is entering the correct data. I have about 95% of this done but I can not figure out the Continue? (y/n) part. If you hit anything but y or n (or if you leave the line blank) it is supposed to print an error:So this is what the application is supposed to look like in the console:
Continue? (y/n):
Error! This entry is required. Try again.
Continue? (y/n): x
Error! Entry must be 'y' or 'n'. Try again.
Continue? (y/n): n
Here is my code:
public static void main(String[] args) {
System.out.println("Welcome to the Loan Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0, 1000000);
double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double monthlyPayment = calculateMonthlyPayment(loanAmount, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
String results = "Loan amount: \t\t" + currency.format(loanAmount) + "\n"
+ "Yearly interest rate: \t" + percent.format(interestRate/100) + "\n"
+ "Number of years: \t" + years + "\n"
+ "Monthly payment: \t" + currency.format(monthlyPayment) + "\n";
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
String getContinue = getContinueWithinRange(sc, "Continue? (y/n): ", "y", "n");
System.out.print(getContinue);
System.out.println();
}
}
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <=min) {
System.out.println("Error! Number must be greater than " + min + ".");
}
else if (d >= max) {
System.out.println("Error! Number must be less than " + max + ".");
}
else {
isValid = true;
}
}
return d;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try Again.");
}
sc.nextLine();
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min) {
System.out.println("Error! Number must be greater than " + min + ".");
}
else if (i >= max) {
System.out.println("Error! Number must be less than " + max + ".");
}
else {
isValid = true;
}
}
return i;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if(sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
}
return i;
}
public static double calculateMonthlyPayment(double loanAmount, double monthlyInterestRate, double months)
{
double monthlyPayment = 0;
for (int i = 1; i <= months; i++)
{
monthlyPayment = loanAmount * monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, months));
}
return monthlyPayment;
}
System.out.print(getContinue);
System.out.println();
public static String getContinueWithinRange(Scanner sc, String prompt, String yContinue, String nContinue)
{
String i = "";
boolean isValid = false;
while (isValid == false)
{
i = getContinue(sc, prompt);
if (!yContinue.equalsIgnoreCase("") && !nContinue.equalsIgnoreCase("")){
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
else{
isValid = true;
}
}
return i;
}
public static String getContinue(Scanner sc, String prompt)
{
String i = "";
boolean isValid = false;
while(isValid == false)
{
System.out.print(prompt);
if (i.length() > 0)
{
i = sc.nextLine();
isValid = true;
}
else
{
System.out.println("Error! Entry is required. Try again.");
}
sc.nextLine();
}
return i;
}
}

Change the content of the while loop in getContinue() to this:
System.out.print(prompt);
i = sc.nextLine();
if (i.length() > 0)
{
isValid = true;
}
else
{
System.out.println("Error! Entry is required. Try again.");
}
This first prints the prompt, then reads the input into the variable that will be returned, then checks whether the input had length greater than zero.
In getContinueWithinRange(), the condition in the if clause needs to be replaced by
!yContinue.equalsIgnoreCase(i) && !nContinue.equalsIgnoreCase(i)
This will "y" and "n" against the input instead of against "".
Also if you would like to actually continue after reading a "y", you need to do something like this:
if (!yContinue.equalsIgnoreCase(i) && !nContinue.equalsIgnoreCase(i)){
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
else {
isValid = true;
}
The first case catches invalid input, the second ends the loop if the user entered "n", the third tells the user the loop will continue after he entered a "y".
Finally, to make your program do what it's supposed to do, change
String getContinue = getContinueWithinRange(sc, "Continue? (y/n): ", "y", "n");
to
choice = getContinueWithinRange(sc, "Continue? (y/n): ", "y", "n");
in your main method.

I try something for you according to your first submitted code
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Tester4 {
public static String getContinueWithinRange(Scanner sc, String prompt, String yContinue, String nContinue) {
String result = "";
boolean isValid = false;
boolean isStarted = false;
while(!isValid) {
result = getContinue(sc, prompt, isStarted);
isStarted = true;
if(yContinue.equalsIgnoreCase(result) || nContinue.equalsIgnoreCase(result)) {
isValid = true;
} else if(!yContinue.equalsIgnoreCase(result) || !nContinue.equalsIgnoreCase(result)) {
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
} else {
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
}
}
return result;
}
public static String getContinue(Scanner sc, String prompt, boolean isStarted) {
String result = "";
boolean isValid = false;
while(!isValid) {
if(result.isEmpty() && !isStarted) {
System.out.print(prompt);
System.out.println("Error! Entry is required. Try again.");
}
result = sc.nextLine();
if(result.length() > 0) {
isValid = true;
}
}
return result;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String getContinue = getContinueWithinRange(sc, "Continue? (y/n): ", "y", "n");
// to call the method
System.out.print(getContinue);
System.out.println();
}
// InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");
// BufferedImage bufferedImage=ImageIO.read(stream);
// ImageIcon icon= new ImageIcon(bufferedImage);
}

import java.util.*;
import java.text.*;
public class LoanPaymentApp {
public static void main(String[] args)
{
System.out.println("Welcome to the Loan Calculator");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0, 1000000);
double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double monthlyPayment = calculateMonthlyPayment(loanAmount, monthlyInterestRate, months);
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
String results = "Loan amount: \t\t" + currency.format(loanAmount) + "\n"
+ "Yearly interest rate: \t" + percent.format(interestRate/100) + "\n"
+ "Number of years: \t" + years + "\n"
+ "Monthly payment: \t" + currency.format(monthlyPayment) + "\n";
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
System.out.print("Continue? (y/n): ");
choice = sc.nextLine();
while (!choice.equalsIgnoreCase("y") && !choice.equalsIgnoreCase("n"))
{
if (choice.equals(""))
{
System.out.println("Error! This entry is required. Try again.");
System.out.print("Continue? (y/n): ");
}
else
{
System.out.println("Error! Entry must be 'y' or 'n'. Try again.");
System.out.print("Continue? (y/n): ");
}
choice = sc.nextLine();
}
}
}
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <=min) {
System.out.println("Error! Number must be greater than " + min + ".");
}
else if (d >= max) {
System.out.println("Error! Number must be less than " + max + ".");
}
else {
isValid = true;
}
}
return d;
}
public static double getDouble(Scanner sc, String prompt)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try Again.");
}
sc.nextLine();
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt, int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min) {
System.out.println("Error! Number must be greater than " + min + ".");
}
else if (i >= max) {
System.out.println("Error! Number must be less than " + max + ".");
}
else {
isValid = true;
}
}
return i;
}
public static int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if(sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine();
}
return i;
}
public static double calculateMonthlyPayment(double loanAmount, double monthlyInterestRate, double months)
{
double monthlyPayment = 0;
for (int i = 1; i <= months; i++)
{
monthlyPayment = loanAmount * monthlyInterestRate/(1 - 1/Math.pow(1 + monthlyInterestRate, months));
}
return monthlyPayment;
}
}

Related

Why am I getting these no suitable method found errors?

Im trying to write this class copied from my textbook. From what i understand, I copied the code exactly as it appeared, with minor changes to variable names. But when I do anything, the two lines number = getInt(Sc3, prompt); and d = getDouble(sc3, prompt); throw errors that say "no suitable method found for getInt/getDouble(Scanner, String). Why is this and what can I code to fix this? The surrounding code below.
import java.util.Scanner;
public class Console {
private static Scanner sc3 = new Scanner(System.in);
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc3.next();
sc3.nextLine();
return s;
}
public static int getInt(String prompt) {
int number = 0;
boolean isValid = false;
while (isValid) {
System.out.print(prompt);
if (sc3.hasNextInt()) {
number = sc3.nextInt();
isValid = true;
}else {
System.out.println("Error! Entry must be an integer. Please try again.");
}
sc3.nextLine();
}
return number;
}
public static int getInt(String prompt, int minimum, int maximum) {
int number = 0;
boolean isValid = false;
while (!isValid) {
number = getInt(sc3, prompt);
if (number <= minimum) {
System.out.println("Error. User entry nust be greater than" + " " + minimum + " " + ".");
} else if (number >= maximum) {
System.out.println("Error. User entry must be less than" + " " + maximum + " " + ".");
} else {
isValid = true;
}
}
return number;
}
public static double getDouble(String prompt) {
double d = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc3.hasNextDouble()) {
d = sc3.nextDouble();
isValid = true;
} else {
System.out.println("Error. Invalid number. Try again.");
}
sc3.nextLine();
}
return d;
}
public static double getDouble(String prompt, double minimum, double maximum) {
double d = 0;
boolean isValid = false;
while (!isValid) {
d = getDouble(sc3, prompt);
if (d <= minimum) {
System.out.println("Error. Number must be greater than" + minimum + ".");
}else if (d >= maximum) {
System.out.println("Error. Number must be less than" + maximum + ".");
}else {
isValid = true;
}
return d;
}
}
}
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists
from here
So you are missing the getInt(Scanner Sc3, String prompt) and getDouble(Scanner sc3, String prompt) methods.

Change the askToContinue method so that it requires the user to enter Y, y, N or n

Having a problem getting the program to keep running after an error message has occurred. I have tried using different If/Else or While loops to get it to work but none seem to be giving any luck. The current code posted works until you enter something other than y, Y, n, or N. The error message will keep popping up after even if i enter y, Y, n, or N.
import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FutureValueApp {
public static void main(String[] args) {
// Avery Owen
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();
// calculate the future value
double monthlyInterestRate = interestRate / 12 / 100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
// print the results
System.out.println("FORMATTED RESULTS");
printFormattedResults(monthlyInvestment, interestRate,
years, futureValue);
// see if the user wants to continue
choice = askToContinue(sc);
}
}
public static double getDoubleWithinRange(Scanner sc, String prompt,
double min, double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(sc, prompt);
if (d <= min) {
System.out.println(
"Error! Number must be greater than " + min + ".");
} else if (d >= max) {
System.out.println(
"Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return d;
}
public static double getDouble(Scanner sc, String prompt) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid number. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt,
int min, int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(sc, prompt);
if (i <= min) {
System.out.println(
"Error! Number must be greater than " + min + ".");
} else if (i >= max) {
System.out.println(
"Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return i;
}
public static int getInt(Scanner sc, String prompt) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
try {
i = sc.nextInt();
isValid = true;
} catch (InputMismatchException e) {
System.out.println("Error! Enter a whole number. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months) {
double futureValue = 0;
for (int i = 1; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
public static String askToContinue(Scanner sc) {
System.out.print("Continue? (y/n): ");
String choice = sc.next();
while (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")){
sc.nextLine(); // discard any other data entered on the line
System.out.println("Error! Enter y or n. Try again.");
}
return choice;
}
private static void printFormattedResults(double monthlyInvestment,
double interestRate, int years, double futureValue) {
// get the currency and percent formatters
NumberFormat c = NumberFormat.getCurrencyInstance();
NumberFormat p = NumberFormat.getPercentInstance();
p.setMinimumFractionDigits(1);
// format the result as a single string
String results
= "Monthly investment: " + c.format(monthlyInvestment) + "\n"
+ "Yearly interest rate: " + p.format(interestRate / 100) + "\n"
+ "Number of years: " + years + "\n"
+ "Future value: " + c.format(futureValue) + "\n";
// print the results
System.out.println(results);
}
}
Try this:
public static String askToContinue(Scanner sc) {
System.out.print("Continue? (y/n): ");
String choice = sc.next();
while (!choice.equalsIgnoreCase("n") && !choice.equalsIgnoreCase("y")){
sc.nextLine(); // discard any other data entered on the line
System.out.println("Error! Enter y or n. Try again.");
System.out.print("Continue? (y/n): "); //this was missing
choice = sc.next(); //take the input again
}
return choice;
}
You are getting stuck in a while loop in case of invalid input and not accepting the input again.

First do-while loop not functioning properly

There seems to be something with my code. My code detects non-integers and keep asking the user to input a positive #, but when you insert a negative #, it just ask the user to input a positive just once. What is wrong? Something with my do-while loop for sure.
I'm just focusing on the firstN, then I could do the secondN.
import java.util.Scanner;
public class scratch {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstN = 0;
int secondN = 0;
boolean isNumber = false;
boolean isNumPos = false;
System.out.print("Enter a positive integer: ");
do {
if (input.hasNextInt()) {
firstN = input.nextInt();
isNumber = true;
}
if (firstN > 0) {
isNumPos = true;
isNumber = true;
break;
} else {
isNumPos = false;
isNumber = false;
System.out.print("Please enter a positive integer: ");
input.next();
continue;
}
} while (!(isNumber) || !(isNumPos));
System.out.print("Enter another positive integer: ");
do {
if (input.hasNextInt()) {
secondN = input.nextInt();
isNumber = true;
}
if (secondN > 0) {
isNumPos = true;
isNumber = true;
} else {
isNumPos = false;
isNumber = false;
System.out.print("Please enter a positive integer: ");
input.next();
}
} while (!(isNumber) || !(isNumPos));
System.out.println("The GCD of " + firstN + " and " + secondN + " is " + gCd(firstN, secondN));
}
public static int gCd(int firstN, int secondN) {
if (secondN == 0) {
return firstN;
} else
return gCd(secondN, firstN % secondN);
}
}
You read the input twice in such case:
firstN = input.nextInt();
...
input.next ();
Add some indication variable or reorganize the code so when you read via first read, avoid the second one.
Try this piece of code
while (true){
System.out.println("Please enter Positive number");
boolean hasNextInt = scanner.hasNextInt();
if(hasNextInt){
int firstNum = scanner.nextInt();
if(firstNum>0){
break;
}else{
continue;
}
}else{
scanner.next();
continue;
}
}

Only accept positive integers using while loop or any other

My program currently rejects any inputs other than an integer, but I'm trying to take it one step further and have it accept only positive integers. How would i accomplish this. I tried creating another boolean and calling it numisGreat and another do-while loop for this part, but I'm cant seem to get it.
import java.util.Scanner;
public class TandE {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int firstN = 0;
int secondN = 0;
boolean isNumber = false;
boolean numisGreat = false;
System.out.print("Enter a positive integer: ");
do {
if (input.hasNextInt()) {
firstN = input.nextInt();
isNumber = true;
} else {
System.out.print("Please enter a positive integer: ");
isNumber = false;
input.next();
}
} while (!(isNumber));
System.out.print("Enter another positive integer: ");
do {
if (input.hasNextInt()) {
secondN = input.nextInt();
isNumber = true;
} else {
System.out.print("Please enter a positive integer: ");
isNumber = false;
input.next();
}
} while (!(isNumber));
System.out.println("The GCD of " + firstN + " and " + secondN + " is " + gCd(firstN, secondN));
}
public static int gCd(int firstN, int secondN) {
if (secondN == 0) {
return firstN;
} else
return gCd(secondN, firstN % secondN);
}
}
Just add a condition that tests if the input number is positive :
boolean isPosNumber = false;
do {
if (input.hasNextInt()) {
firstN = input.nextInt();
if (firstN > 0) {
isPosNumber = true;
} else {
System.out.println("You entered a non-positive number, try again: ");
}
} else {
System.out.print("Please enter a positive integer: ");
isPosNumber = false;
input.next();
}
} while (!isPosNumber);

How do I get the error "non-static method GetString(Scanner,String) cannot be refereneced from a static context" to go away? [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 9 years ago.
I'm working with two classes. One is called Validator and one is called LineItemApp.
Here is the code for the Validator class.
import java.util.Scanner;
public class Validator
{
public String getString(Scanner sc, String prompt)
{
System.out.print(prompt);
String s = sc.next(); // read user entry
sc.nextLine(); // discard any other data entered on the line
return s;
}
public int getInt(Scanner sc, String prompt)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public int getInt(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return i;
}
public double getDouble(Scanner sc, String prompt)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public double getDouble(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min + ".");
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max + ".");
else
isValid = true;
}
return d;
}
}
Here is my LineItemApp class
import java.util.Scanner;
public class LineItemApp
{
public static void main(String args[])
{
// display a welcome message
System.out.println("Welcome to the Line Item Calculator");
System.out.println();
// create 1 or more line items
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
// get the input from the user
String productCode = Validator.getString(sc,
"Enter product code: ");
int quantity = Validator.getInt(sc,
"Enter quantity: ", 0, 1000);
// get the Product object
Product product = ProductDB.getProduct(productCode);
// create the LineItem object
LineItem lineItem = new LineItem(product, quantity);
// display the output
System.out.println();
System.out.println("LINE ITEM");
System.out.println("Code: " + product.getCode());
System.out.println("Description: " + product.getDescription());
System.out.println("Price: " + product.getFormattedPrice());
System.out.println("Quantity: " + lineItem.getQuantity());
System.out.println("Total: " +
lineItem.getFormattedTotal() + "\n");
// see if the user wants to continue
choice = Validator.getString(sc, "Continue? (y/n): ");
System.out.println();
}
}
}
Any help would be greatly appreciated.
Thanks,
David
You need an instance of Validator, to enter the methods
// see if the user wants to continue
Validator validator = new Validator();
choice = validator.getString(sc, "Continue? (y/n): ");
System.out.println();
Or as suggested in another answer you can make Validator methods static, but this change sure is bigger and you have to change code, and you don't know if you use this validator in another part so im not agree but if you want to refactor it, is not at all bad idea cause your class Validator doesn't have state, only do some algorithms.
You are calling non-static methods from a static method (which is now main() ).
You can eliminate the error by declaring the methods arising the warning to static.
Your should understand what is the static method.
http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

Categories

Resources