Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, October 20, 2016

Calling a formatting method

Calling a formatting method


I am very confused on how to calling a format method so that a String is printed with an int within it.

//call  boatPrice = inputBoatPrice("Please enter the price of the boat, must be > :", 0.0);    //method  public static double inputBoatPrice(String messagePassed, double limitPassed) {      Scanner keyboard = new Scanner(System.in);      int userInput;      do {          System.out.printf("%1s %1.2f\n", messagePassed, limitPassed);          userInput = keyboard.nextInt();      }  while(userInput < limitPassed);      return userInput;  } //end method  

How can I fix this so that the call prints out:

"Please enter the price of the boat, must be > 0.0:"

Currently it prints out

"Please enter the price of the boat, must be > : 0.00"

Answer by nullpointer for Calling a formatting method


EDIT :: I think all you want to do is print in a specific format of 1.1 with that colon at the end. Then you need this :

public static void main(String[] args) {          inputBoatPrice("Please enter the price of the boat, must be >",0.0);      }        public static double inputBoatPrice(String messagePassed, double limitPassed) {          Scanner keyboard = new Scanner(System.in);          int userInput;          do {              System.out.printf("%1s %1.1f :\n", messagePassed, limitPassed);              userInput = keyboard.nextInt();          }  while(userInput < limitPassed);          return userInput;      }  

Answer by Abdelhak for Calling a formatting method


Try to use this I adjust your printf and right typr of return:

  public static double inputBoatPrice(String messagePassed, double limitPassed)    {        Scanner keyboard = new Scanner(System.in);        double userInput;    do {          System.out.printf("%1s %1.1f\n", messagePassed, limitPassed);            userInput = keyboard.nextDouble();         } while(userInput < limitPassed);           return userInput;    }  

Answer by Aron_dc for Calling a formatting method


Just change

System.out.printf("%1s %1.2f\n", messagePassed, limitPassed);  

to

System.out.printf(messagePassed, limitPassed);  

and your String to

"Please enter the price of the boat, must be > %1.1f :"  

This

helps to correct the problem with your string.

You also have to adjust your userInput variable, as you want to read oubles (at least you ask for double values and therefore you should accept only double values). This means changing the type of userInput as well as keyboard.nextInt(); to keyboard.nextDouble();

Answer by ChristophE for Calling a formatting method


just adapt your printf to:

System.out.printf("%1s %1.1f\n", messagePassed, limitPassed);  

Answer by Ravers for Calling a formatting method


I made some adjustments to your code:

double boatPrice = inputBoatPrice("Please enter the price of the boat, must be > ", 0.0);    public static double inputBoatPrice(String messagePassed, double limitPassed) {      Scanner keyboard = new Scanner(System.in);      int userInput;      do {           System.out.print(messagePassed + limitPassed + ":");           userInput = keyboard.nextInt();      }  while(userInput < limitPassed);        return userInput;  }  

You need to change your System.out.print from:

System.out.printf("%1s %1.2f\n", messagePassed, limitPassed);  

to:

System.out.print(messagePassed + limitPassed + ":");  

Also edit the string that you pass when you call the method inputBoatPrice:

("...must be > :") to ("...must be > ")  

Answer by Nicholas for Calling a formatting method


Your format string contains %1.2f, which means print a minimum of one digit total and 2 digits after the period. Changing it to %1.1f will mean print a minimum of 1 digit total and 1 digit after the period.

You will find this in the Javadoc under Formatter. The first number is the width and the second number is the precision. From the Javadoc:

Width

The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.

Precision

For general argument types, the precision is the maximum number of characters to be written to the output.

For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.