Reputation: 27
I have this code, and the input in the textfield is 50, so its output should be 25,
but there is a error, because no output
button_1.addActionListener(new ActionListener() {
String tfg = textField.getText().toString();
String tfc = textField.getText();
public void actionPerformed(ActionEvent e) {
if(textField.getText().toString().equals("0") ||
textField.getText().toString().equals("") ||
textField.getText().toString().equals(" ") ||
textField.getText().toString().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc);
int trcs = tfgg * 1;
double trcp = 1 / 2;
int trcc = (int) trcp * trcs;
System.out.println(new Integer(trcc).toString());
} catch (NumberFormatException se) {
}
}
}
});
I try to convert from my php to that code, here is my php code :
$doorSeed = $mount * 1;
$doorPrice = 1 / 2;
$doorConvert = $doorPrice * $doorSeed;
echo "Treasure Chest Seed = ".$doorSeed." Price : ".$doorConvert." World Lock <br>";
Upvotes: 0
Views: 146
Reputation: 13402
int trcc = (int) trcp * trcs;
Try this instead
int trcc = (int) (trcp * trcs);
You need to cast the complete expression
[(trcp * trcs)
] instead of just the first variable trcp
to int
. The other variable in the expression trcs
is of double
type, so the result of the expression become double
. That is why you cast the complete expression. So your end result will be int
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4; tfgg = 4;
int trcs = tfgg * 1; // trcs = 4 * 1 = 5;
double trcp = 1.0 / 2; // trcp = 0.5;
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2;
System.out.println(trcc); // 2;
double trcp = 1.0 / 2;
Here make at least one value as double
so the expression will be evaluated as double
as suggested by @tunaki.
Also you don't need this statement in your code int trcs = tfgg * 1;
.
Use like this:
String tfg ;
String tfc ;
public void actionPerformed(ActionEvent e) {
tfg = textField.getText();
tfc = textField.getText();
if(textField.getText().equals("0") || textField.getText().equals("") || textField.getText().equals(" ") || textField.getText().matches("[a-zA-Z]+")) {
JOptionPane.showMessageDialog(Cow.this,
"Tree Amount Should Be 1 or More",
"Invalid Tree Amount",
JOptionPane.ERROR_MESSAGE);
}
if(!tfg.equals("0")) {
try {
int tfgg = Integer.parseInt(tfc.trim()); // tfc = 4;
int trcs = tfgg; // trcs = 4 * 1 = 5
double trcp = 1.0 / 2; // trcp = 0.5
int trcc = (int) (trcp * trcs); // trcc = (0.5 * 5) = (2.5) = 2
System.out.println("HI: "+trcc);
} catch (NumberFormatException ignored) {
}
Upvotes: 2
Reputation: 137319
Your problem is at this line:
double trcp = 1 / 2;
This does not result in trcp = 0.5
but trcp = 0.0
, because you are dividing integer values (and so are using integer division).
You should use the following code:
double trcp = 1.0 / 2;
to force division using doubles.
Other comments:
new Integer(trcc).toString()
should be replaced with String.valueOf(trcc)
.int trcs = tfgg * 1;
?Upvotes: 1
Reputation: 462
Basically you have this (int)0.5 = 0
.
In your case, trcp = 0.5
and is double, so when it's cast to integer the result is 0.
And when you do (int)a + b
, the true operation with parenthesis for priority here is: ((int)a)+(b)
, so what you have to is the following:
int trcc = (int) (trcp * trcs);
instead of the following:
int trcc = (int) trcp * trcs;
Upvotes: 0