Home > Software engineering >  Adding a String into a scanner
Adding a String into a scanner

Time:11-10

//I wanted to insert a decimal number to console (like 1.85) to the String groesse and I also wanted it to be in the equation of bmi. How do i do that?

public static void main(String[] args) {
    Locale.setDefault(Locale.US);
    PrintWriter out = new PrintWriter(System.out);

bmiRechner(out);

out.flush();
}

public static void bmiRechner(PrintWriter out) {
    Scanner sc = new Scanner (System.in);
    
    String = groesse;
    
    out.println("Schönen Guten Tag");
    out.flush();
    out.println("Geben Sie ihre Körpergröße in Metern an");
    out.flush();
    groesse = sc.next();
    out.println("Geben Sie nun ihr Körpergewicht in Kilogramm ein");
    out.flush();
    int gewicht = sc.nextInt();
    
    double bmi = gewicht / Math.pow(groesse, 2);
    
    out.println("Das ist Ihr BMI: "   bmi);
    
}

}

CodePudding user response:

How do i do that?

OK, so you want to read user input from the console. To do so, instead of ...

groesse = sc.next();

use

// This line will get the input line 
groesse = sc.nextLine().trim();

// This will get the numeric value of the input
double groesse = Double.valueOf(groesse)

The same applies to the other measurement.

  • Related