But as this is java we need to make it a little more complicated. I'm here heavily relying on @Uri Agassi's answer @Uri Agassi's answer
But as this is java we need to make it a little more complicated. I'm here heavily relying on @Uri Agassi's answer
But as this is java we need to make it a little more complicated. I'm here heavily relying on @Uri Agassi's answer
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
//this is the c# attempt...
valid = tryParse(sc.nextLine(), out value);
//You would need to do something like this in java
Double result = tryParse(sc.nextLine());
valid = result != null;
value = (double)result;
}
return value;
}
tryParse would then (in C#) return true, when parsing the number was successful. YouYou'd just need to assign to an out parameter before returning in the method. this means you'd just need to change the method a small bit:
Please keep in mind, that Java does not suppot pass by reference..But as this means. The code that is posted the e e does not workjava we need to make it a little more complicated. I'm here heavily relying on @Uri Agassi's answer
private Double tryParse(String text){
try{
Double value = new Double(text);
return value;
}
catch (NumberFormatException e){
if(text.matches("/^quit$/gi"))
Application.Exit;
else
return null;
}
}
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
valid = tryParse(sc.nextLine(), out value);
}
return value;
}
tryParse would then return true, when parsing the number was successful. You just need to assign to an out parameter before returning in the method. this means you'd just need to change the method a small bit:
Please keep in mind, that Java does not suppot pass by reference.. this means. The code that is posted the e e does not work
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
//this is the c# attempt...
valid = tryParse(sc.nextLine(), out value);
//You would need to do something like this in java
Double result = tryParse(sc.nextLine());
valid = result != null;
value = (double)result;
}
return value;
}
tryParse would then (in C#) return true, when parsing the number was successful. You'd just need to assign to an out parameter before returning in the method:
But as this is java we need to make it a little more complicated. I'm here heavily relying on @Uri Agassi's answer
private Double tryParse(String text){
try{
Double value = new Double(text);
return value;
}
catch (NumberFormatException e){
if(text.matches("/^quit$/gi"))
Application.Exit;
else
return null;
}
}
A few things jump me here:
###1. Class Names
Your class name is queer. The Java standard is to use PascalCasing
for class names. You use Pascal_Snake_Case
. That's kinda funny ;) Also you named your class after what it contains somewhere deep down, and not what it's responsible for. I'd instead probably name it Program. As it's nothing else, than that. I'd expect a class named Quadratic_Equations
to be an enum containing different Equations.
###2. checkIfValidNumber
I like the name. It's good, it could describe what it does. I don't like the print-statement and that you have a loop in there. This is one of these "side-effects". Your function name makes me expect something different. I expect a function applying some constraint to a number and returning true or false depending on the validity of that number to the constraint.
You also hide your whole process behind that. Also: why should a user not enter -1.0
for your quadratic equation? It makes no sense to use a number within the valid range to check for validity. instead use boolean return type in your tryParse and make use of the out-parameter.
###3. Reading user input.
double a = tryParse(sc.nextLine());
a = checkValidNumber(a);
This makes no sense. You should also move the first tryParse(sc.nextLine());
into a method.
Instead I would expect:
double a = promptUserUntilValidInput("Please enter a:");
You can move the whole reading to this method.
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
valid = tryParse(sc.nextLine(), out value);
}
return value;
}
tryParse would then return true, when parsing the number was successful. You just need to assign to an out parameter before returning in the method. this means you'd just need to change the method a small bit:
private boolean tryParse(String text, out double val){
try{
val = new Double(text);
return true;
}
catch (NumberFormatException e){
return false;
}
}
This way you also eliminate all checks for -1.0
.
Please keep in mind, that Java does not suppot pass by reference.. this means. The code that is posted the e e does not work
A few things jump me here:
###1. Class Names
Your class name is queer. The Java standard is to use PascalCasing
for class names. You use Pascal_Snake_Case
. That's kinda funny ;) Also you named your class after what it contains somewhere deep down, and not what it's responsible for. I'd instead probably name it Program. As it's nothing else, than that. I'd expect a class named Quadratic_Equations
to be an enum containing different Equations.
###2. checkIfValidNumber
I like the name. It's good, it could describe what it does. I don't like the print-statement and that you have a loop in there. This is one of these "side-effects". Your function name makes me expect something different. I expect a function applying some constraint to a number and returning true or false depending on the validity of that number to the constraint.
You also hide your whole process behind that. Also: why should a user not enter -1.0
for your quadratic equation? It makes no sense to use a number within the valid range to check for validity. instead use boolean return type in your tryParse and make use of the out-parameter.
###3. Reading user input.
double a = tryParse(sc.nextLine());
a = checkValidNumber(a);
This makes no sense. You should also move the first tryParse(sc.nextLine());
into a method.
Instead I would expect:
double a = promptUserUntilValidInput("Please enter a:");
You can move the whole reading to this method.
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
valid = tryParse(sc.nextLine(), out value);
}
return value;
}
tryParse would then return true, when parsing the number was successful. You just need to assign to an out parameter before returning in the method. this means you'd just need to change the method a small bit:
private boolean tryParse(String text, out double val){
try{
val = new Double(text);
return true;
}
catch (NumberFormatException e){
return false;
}
}
This way you also eliminate all checks for -1.0
.
A few things jump me here:
###1. Class Names
Your class name is queer. The Java standard is to use PascalCasing
for class names. You use Pascal_Snake_Case
. That's kinda funny ;) Also you named your class after what it contains somewhere deep down, and not what it's responsible for. I'd instead probably name it Program. As it's nothing else, than that. I'd expect a class named Quadratic_Equations
to be an enum containing different Equations.
###2. checkIfValidNumber
I like the name. It's good, it could describe what it does. I don't like the print-statement and that you have a loop in there. This is one of these "side-effects". Your function name makes me expect something different. I expect a function applying some constraint to a number and returning true or false depending on the validity of that number to the constraint.
You also hide your whole process behind that. Also: why should a user not enter -1.0
for your quadratic equation? It makes no sense to use a number within the valid range to check for validity. instead use boolean return type in your tryParse and make use of the out-parameter.
###3. Reading user input.
double a = tryParse(sc.nextLine());
a = checkValidNumber(a);
This makes no sense. You should also move the first tryParse(sc.nextLine());
into a method.
Instead I would expect:
double a = promptUserUntilValidInput("Please enter a:");
You can move the whole reading to this method.
private double promptUserUntilValidInput(String prompt){
boolean valid = false;
double value = 0.0;
while(!valid){
System.out.println(prompt);
valid = tryParse(sc.nextLine(), out value);
}
return value;
}
tryParse would then return true, when parsing the number was successful. You just need to assign to an out parameter before returning in the method. this means you'd just need to change the method a small bit:
private boolean tryParse(String text, out double val){
try{
val = new Double(text);
return true;
}
catch (NumberFormatException e){
return false;
}
}
This way you also eliminate all checks for -1.0
.
Please keep in mind, that Java does not suppot pass by reference.. this means. The code that is posted the e e does not work