Write a method with the following header to format the integerwith the specified width.public static String format(int number, int width)The method returns a string for the number with one or more prefix 0s. The size of thestring is the width. For example, format(34, 4) returns 0034 and format(34,5) returns 00034. If the number is longer than the width, the method returns thestring representation for the number. For example, format(34, 1) returns 34.Write a test program that prompts the user to enter a number and its width, anddisplays a string returned by invoking format(number, width).

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 17RQ
icon
Related questions
Question

Write a method with the following header to format the integer
with the specified width.
public static String format(int number, int width)
The method returns a string for the number with one or more prefix 0s. The size of the
string is the width. For example, format(34, 4) returns 0034 and format(34,
5) returns 00034. If the number is longer than the width, the method returns the
string representation for the number. For example, format(34, 1) returns 34.
Write a test program that prompts the user to enter a number and its width, and
displays a string returned by invoking format(number, width).

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Methods of StringBuilder class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT