Reputation: 481
I have a problem in Java. I'm trying to check if the input provided only contains decimal number (Number and a ".") This code, provided by my professor, its not really evaluating. And I really can't figure out what is wrong.
import javax.swing.JOptionPane;
public class MoneyCount {
public static void check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
}
} else {
JOptionPane.showMessageDialog(null,
"You must enter an integer value");
System.exit(0);
}
}
}
public static void main(String[] args) {
//retrieve amount due
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
check(moneyd);
double amountd = Double.parseDouble(moneyd) * 100;
String moneyr = JOptionPane.showInputDialog("Enter amount you would like to pay");
check(moneyr);
double amountr = Double.parseDouble(moneyr) * 100;
}
}
Upvotes: 0
Views: 134
Reputation: 446
Check this one
public class Test {
public static void main(String[] args) throws ParseException {
String moneyd = JOptionPane.showInputDialog("Enter the amount due");
if (check(moneyd)) {
double amountd = Double.parseDouble(moneyd) * 100;
System.out.println("ok");
} else {
System.out.println("Not a valid number");
System.exit(0);
}
}
public static boolean check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length(); i = i + 1) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
} else {
return (false);
}
}
}
return true;
}
}
Upvotes: 0
Reputation: 496
You can use the try and catch to verify if the string is a decimal or not like the above code:
public static void check(String s) {
try {
Integer.parseInt(s);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"You must enter an integer value");
System.exit(0);
}
}
Upvotes: 0
Reputation: 97
What do you mean by "its not really evaluating".
The way I understand the code :
Upvotes: 0
Reputation: 347184
Start with a desk check (oh dear lord, I never thought I'd say that again)...
Basically all this method does is check to see if the s
contains more than one .
character
Starting with s
equal to 123.123
decimalPoint
= false
i
= 0
, s.charAt(i)
will be 1
.i
= 1
, s.charAt(i)
will be 2
.i
= 2
, s.charAt(i)
will be 3
.i
= 3
, s.charAt(i)
will be .
.decimalPoint
is false
, change decimalPoint
to true
i
= 4
, s.charAt(i)
will be 1
.i
= 5
, s.charAt(i)
will be 2
.i
= 6
, s.charAt(i)
will be 3
.Starting with s
equal to 1.2.3
decimalPoint
= false
i
= 0
, s.charAt(i)
will be 1
.i
= 1
, s.charAt(i)
will be .
.decimalPoint
is false
, change decimalPoint
to true
i
= 2
, s.charAt(i)
will be 2
.i
= 3
, s.charAt(i)
will be .
.decimalPoint
is true
, show error message and exitStarting with s
equal to 123
decimalPoint
= false
i
= 0
, s.charAt(i)
will be 1
.i
= 1
, s.charAt(i)
will be 2
.i
= 2
, s.charAt(i)
will be 3
.decimalPoint
is false
So the only case which breaks is the second one...Note, this method does not take into consideration what might occur if you enter alpha characters...but that's not what it's designed for...
Upvotes: 0
Reputation: 51
Is the problem to figure out what's wrong with your professor's code or to write your own method?
If it's the former, I'd also point out the intent of the error message should be "You must enter a decimal value".
Upvotes: 1
Reputation: 446
try changing your code like this
public static void check(String s) {
boolean decimalPoint = false;
for (int i = 0; i < s.length();i++) {
if (s.charAt(i) == '.') {
if (!decimalPoint) {
decimalPoint = true;
}else{
JOptionPane.showMessageDialog(null,
"Not a valid number (contains more than one .)");
System.exit(0);
}
}
}
if (decimalPoint) {
{
JOptionPane.showMessageDialog(null,
"You must enter an integer value");// you need only integer as input
System.exit(0);
}
}
}
you must show the message only after checking the whole string. That is after completing the loop
Upvotes: 0