Create a Java program that includes a function called isNumber() to receive a text parameter from the user and determine whether the text represents a number or not.
isNumber()
true
false
234
import java.util.Scanner; public class NumberChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a text:"); String userInput = scanner.nextLine(); // Call the isNumber function and display the result boolean isNumber = isNumber(userInput); System.out.println(isNumber); scanner.close(); } // Function to check if a text is a number static boolean isNumber(String text) { try { // Try to convert the text to a number Double.parseDouble(text); // If no exception is thrown, the text is a number return true; } catch (NumberFormatException e) { // If an exception is thrown, the text is not a number return false; } } }
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.