Create a Java program that implements a function called WriteCenteredUnderlined that receives a user-requested text as a parameter. Then, display the text centered and underlined on the screen using the character '-' and assuming we have a width of 80 spaces.
WriteCenteredUnderlined
main
Hello
Hello -----
import java.util.Scanner; public class WriteCenteredUnderlined { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Prompt the user to enter a text System.out.println("Enter a text:"); String userInput = scanner.nextLine(); // Call the WriteCenteredUnderlined function and display the result writeCenteredUnderlined(userInput); scanner.close(); } // Function that writes the text centered and underlined static void writeCenteredUnderlined(String text) { int totalWidth = 80; // Calculate the starting position to center the text int startPosition = (totalWidth - text.length()) / 2; // Print the centered text for (int i = 0; i < startPosition; i++) { System.out.print(" "); } System.out.println(text); // Print the underline with '-' characters for (int i = 0; i < totalWidth; i++) { System.out.print("-"); } System.out.println(); // Line break after the underline } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Develop your Java skills by creating a program with greeting and farewell functions. Learn to execute methods effectively from the main function (Main).
Create a Java program that implements two methods for greeting and saying goodbye. The greeting method should have a text parameter.
Create a Java program that implements a function called 'isAlphabetic()' which takes a text parameter from the user and checks if the text contains alphabetic characters from 'a' to 'z', including both uppercase and lowercase.
Explore text manipulation in Java with this intermediate-level exercise. Implement the changeLetter() function to modify a character in a text. Learn practically and enhance your mastery in programming!
Create a Java program that implements a function called "sum" that returns the sum of two integer numbers passed as parameters.
Create a Java program that implements a function called IsNumber that receives a text parameter from the user and check if the text is a number or not.
Create a Java program that serves as a calculator with the ability to add, subtract, multiply, or divide.
Create a program in Java that implements a function called CountSpaces that receives as a parameter a text requested from the user.
Create a Java program that asks the user for a number and save it in a text string.
Create a new version of the program Main parameter calculator where you can return the following error codes from Main.
Create a Java program that implements a function called FindMinMax that receives three parameters, an array of real numbers and two real numbers.
Create a program in Java that implements a function called WriteCenteredUnderline that receives as a parameter a text requested from the user.
Create a Java program that asks the user for 5 integers to store them in an array of integers.
Create a Java program that implements a function called InvertString receive receive a text by parameter and display them in reverse order.
Create a Java program that implements a function called Double to calculate and return double an integer.
Create a Java program that implements a function to check if a string is a palindrome or not.
Create a program in Java that implements a function called SearchGreatestValue that receives as an parameter an array of 5 real numbers requested from the user.
Create a Java program that implements a function called Power and calculate the result of raising an integer to another number.
Create a Java program that implements a function called WriteTitle that writes a text centered on the screen, in capital letters, with additional spaces and with one line above and another below.
Practice with exercises in Java that use functions of different types. Methods, functions with parameters or values by reference among others.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.