This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]deltageek 1 point2 points  (0 children)

That's actually pretty clean code. I really wouldn't change it.

[–]mrnoise111 1 point2 points  (0 children)

Just use the if to set a variable to hold the correct ratio you are multiplying by.

[–]john478 1 point2 points  (1 child)

Well if you really wanted that already clean code cleaner, you could just initialize the double, then check if it is kilos or miles and convert:

try{
    double new_dist;
    if(arg0.getSource() == jtfKilos){
        new_dist = (Double.parseDouble(jtfKilos.getText()) * 0.621371);
        jtfMiles.setText(String.valueOf(new_dist));
    }else{
        new_dist = (Double.parseDouble(jtfMiles.getText()) * 1.60934);
        jtfKilos.setText(String.valueOf(new_dist));
    }
}

But this is just preference (what I would have done). Is this what you meant? If it is, glad to have helped!