Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a method padString that accepts two parameters: a String and an integer representing a length. The method should pad the parameter string with spaces

Write a method padString that accepts two parameters: a String and an integer representing a length. The method should pad the parameter string with spaces until its length is the given length. For example, padString("hello", 8) should return " hello". (This sort of method is useful when trying to print output that lines up horizontally.) If the string's length is already at least as long as the length parameter, your method should return the original string. For example, padString("congratulations", 10) would return "congratulations".

(Solve this without if statement)

private static String padString(String str, int noOfSpaces) { String newStr=""; int addSpaces; if(str.length()==noOfSpaces) return str; else { addSpaces=noOfSpaces-str.length(); for(int i=1;i<=addSpaces;i++) newStr+=" "; newStr+=str; } return newStr; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago