Skip to content
Advertisement

I want to add a function in a println

I’m trying to add the name input by the user into a printline in Java

 System.out.printf("According to your survey, an average of %.2f people rode the per day.",  media );

I want to add it at the part of the text “rode the…”

Advertisement

Answer

float media = 1.5f;
Scanner stdin = new Scanner(System.in);
System.out.printf("According to your survey, an average of %.2f people rode the %s per day.",
                  media,
                  stdin.nextLine());

When you run the above code, you won’t see anything printed until you enter a value and press ENTER. If, for example, you enter Jabberwocky1 then the following line will be displayed:

According to your survey, an average of 1.50 people rode the Jabberwocky per day.

1Jabberwocky

Advertisement