lundi 16 octobre 2017

detect mistake in my code for validation

I would like to test my code and I want to know what can be failed the compilation of my code I this the problem is in the string character maybe some character is not for string this code calcul the BMI and I want to know what is the problem if they have in my code

import java.util.Scanner;

    public class BMI {
    
        static Scanner sc = new Scanner(System.in);
        static String size; 
        static String weight; 
        static boolean valid;
        static Double p=0.0; 
        static Double t=0.0;
        
            public static boolean isValidDouble(String str) {
                    try {
                            str = str.replaceAll(",", "."); 
                            Double.parseDouble(str);
                    } catch (Exception e) {
                        return false;
                    }
                    return true;        
            }
        
            public static double writeWeight() {
                do{
                    System.out.println("what is your weight?");
                    poids = sc.nextLine(); 
                    
                    valid = isValidDouble(weight); 
                    
                    if(valid){
                        weight = weight.replaceAll(",", "."); 
                        p = Double.parseDouble(weight);
                        if(p < 30){
                            System.out.println("mistake: weight is too  low");
                            valid = false;
                        }
                        if(p > 129){
                            System.out.println("Erreur: weight is to much");
                            valid = false;
                        }
                    }else
                            System.out.println("mistake: the weight should  be number");              
                    
                  }while(valid == false);
                return p;
            }
            
        public static void main(String[] args) {
            
            p = writeWeight();
            
            do{
            System.out.println("what is your size?");
            taille = sc.nextLine(); 
            
            valid = isValidDouble(size); 
            
            if(valid){
            size = size.replaceAll(",", "."); 
            
            t = Double.parseDouble(size); 
            
            if (t > 100)
                t = t/100;
            
            if(t<0.3){
                System.out.println("mistake: size is too low.");
                valide = false;
            }
            if( t>2.10) {
                System.out.println("mistake: size is too much");
                valid = false;
            }
            
            }else
                    System.out.println("mistake: the size should be a number");
            }while(valid == false); 
            
            
            double bmi = p / (t*t); 
            
            System.out.println("BMI is: "+bmi);
        }
        
    }

can you test my code or write a test who can validate my code

Aucun commentaire:

Enregistrer un commentaire