Input All Base Types
Program plan:
- Create a class InputBaseTypes to read different base types of input into standard output device.
- In main() function.
- Invoke the function inputAllBaseTypes() and write the different types of input to output device.
- In the method inputAllBaseTypes(),
- Read the different input values such as integer, decimal, big integer, big decimal, float, Boolean, long, and long integer from user and checks each base type of input value then it prints back to standard output device.
- Use the while loop and predefined function hasNextInt() to check whether the given input is integer or not..
- Use the while loop and predefined function hasNextBigDecimal() to check whether the given input is big decimal or not.
- Use the while loop and predefined function hasNextLong () to check whether the given input is long or not.
- Use the while loop and predefined function hasNextDouble() to check whether the given input is integer or not.
- Use the while loop and predefined function hasNextBoolean() to check whether the given input is Boolean or not.
- Use the while loop and predefined function hasNextBigInteger () to check whether the given input is big integer or not.
- Use the while loop and predefined function hasNextByte () to check whether the given input is byte or not.
- Use the while loop and predefined function hasNextFloat () to check whether the given input is float or not.
- In main() function.
Program to implement the method inputAllBaseTypes() that reads different base type input from input device and print the input back to standard output device.
Explanation of Solution
Program:
//Import the required java package
import java.util.Scanner;
Create a class InputBaseTypes to read different base types of input into standard output device.
//Main class definition
public class InputBaseTypes
{
The main() function prints all types of input into a standard output device.
//Main function
public static void main(String[] args)
{
//Display the header
System.out.println( "Basic input types." );
Invoke the function inputAllBaseTypes() and write the different types of input to output device.
//Call the function inputAllBaseTypes()
System.out.println(inputAllBaseTypes() );
}
Create the method inputAllBaseTypes() that reads the input value from user and checks each base type of input value then it prints back to standard output device.
//Create the method
public static int inputAllBaseTypes()
{
//Read the input from user
Scanner input = new Scanner(System.in);
Get the integer value from user.
//Read the integer value
System.out.print("Enter an integer: ");
Use the while loop and predefined function hasNextInt() to check whether the given input is integer or not. If not, skip the current input and display the error message. If the input is integer, then return back the input to display in standard output device.
/*Call the function hasNextInt() to check the given input is an integer*/
while( !input.hasNextInt() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.print("Not an integer, try again: ");
}
/*Display the entered big decimal into output device.*/
System.out.println( "You entered: " + input.nextInt() );
Get the big decimal from user.
//Read the integer value
System.out.print("Enter a BigDecimal: ");
Use the while loop and predefined function hasNextBigDecimal() to check whether the given input is big decimal or not. If not, skip the current input and display the error message. If the input is big decimal, then return back the input to display in standard output device.
/*Call the function hasNextBigDecimal () to check the given input is an big decimal*/
while( !input.hasNextBigDecimal() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.print("Not a BigDecimal, try again: ");
}
/*Display the entered big decimal into output device*/
System.out.println( "You entered: " + input.nextBigDecimal() );
Get the long value from user.
//Read the long value
System.out.println( "Enter a long: " );
Use the while loop and predefined function hasNextLong() to check whether the given input is long or not. If not, skip the current input and display the error message. If the input is long, then return back the input to display in standard output device.
/*Call the function hasNextLong() to check the given input is a long*/
while( !input.hasNextLong() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a long, try again" );
}
/*Display the entered long value into output device*/
System.out.println( "You entered: " + input.nextLong() );
Get the double value from user.
//Read the double value
System.out.println( "Enter a double: " );
Use the while loop and predefined function hasNextDouble() to check whether the given input is integer or not. If not, skip the current input and display the error message. If the input is integer, then return back the input to display in standard output device.
/*Call the function hasNextDouble() to check the given input is an double*/
while( !input.hasNextDouble() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a double, try again" );
}
/*Display the entered double value into output device.*/
System.out.println( "You entered: " + input.nextDouble() );
Get the Boolean value from user.
//Read the Boolean value
System.out.println( "Enter a boolean: " );
Use the while loop and predefined function hasNextBoolean() to check whether the given input is Boolean or not. If not, skip the current input and display the error message. If the input is Boolean, then return back the input to display in standard output device.
/*Call the function hasNextBoolean () to check the given input is an big decimal*/
while( !input.hasNextBoolean() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a boolean, try again" );
}
//Display the entered boolean into output device
System.out.println( "You entered: " + input.nextBoolean() );
Get the big integer value from user.
//Read the big integer value
System.out.println( "Enter a BigInteger: " );
Use the while loop and predefined function hasNextBigInteger() to check whether the given input is big integer or not. If not, skip the current input and display the error message. If the input is big integer, then return back the input to display in standard output device.
/*Call the function hasNextBigInteger() to check the given input is a long*/
while( !input.hasNextBigInteger() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a BigInteger, try again" );
}
/*Display the entered big integer into output device*/
System.out.println( "You entered: " + input.nextBigInteger() );
Get the input byte from user.
//Read the byte value
System.out.println( "Enter a Byte: " );
Use the while loop and predefined function hasNextByte () to check whether the given input is byte or not. If not, skip the current input and display the error message. If the input is byte, then return back the input to display in standard output device.
/*Call the function hasNextByte() to check the given input is a long*/
while( !input.hasNextByte() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a Byte, try again" );
}
//Display the entered byte into output device
System.out.println( "You entered: " + input.nextByte() );
Get the float value from user.
//Read the float value
System.out.println( "Enter a Float: " );
Use the while loop and predefined function hasNextFloat () to check whether the given input is float or not. If not, skip the current input and display the error message. If the input is float, then return back the input to display in standard output device.
/*Call the function hasNextFloat() to check the given input is a long*/
while( !input.hasNextFloat() )
{
//Return the skipped input
input.nextLine();
//Display the error message
System.out.println( "Not a Float, try again" );
}
//Display the entered float into output device
System.out.println( "You entered: " + input.nextFloat() );
//Return the value
return 0;
}
}
Output:
Basic input types.
Enter an integer: 1234
You entered: 1234
Enter a BigDecimal: 15
You entered: 15
Enter a long:
5
You entered: 5
Enter a double:
2
You entered: 2.0
Enter a boolean:
g
Not a boolean, try again
true
You entered: true
Enter a BigInteger:
89076
You entered: 89076
Enter a Byte:
u
Not a Byte, try again
8
You entered: 8
Enter a Float:
25
You entered: 25.0
0
Want to see more full solutions like this?
Chapter 1 Solutions
Data Structures and Algorithms in Java
- What would the implementation regarding the input section look like in Java?arrow_forwardWrite a program in Java that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first six letters of the first name, followed by the first letter of the last name, an underscore (_), and then the last digit of the number (use the % operator). If the first name has less than six letters, then use all letters of the first name. Ex: If the input is: Michael Jordan 1991 the output is: Your login name: MichaeJ_1 Ex: If the input is: Nicole Smith 2024 the output is: Your login name: NicoleS_4arrow_forwardWrite a JAVA program that checks phrases to determine if they are palindromes. A palindrome is a word, phrase, or sentence that reads left-to-right the same way it reads right-to-left, ignoring all punctuation and capitalization. For example, the statement “Madam, I’m Adam” is a palindrome as the response of “Eve.” Use a method to determine if a String is a palindrome. Test a single word non-palindrome, a single word palindrome, a phrase that is not a palindrome, and a phrase that is a palindrome. Print the phrase and whether it is a palindrome or not. Some simple palindromes are “racecar,” “taco cat,” and “A man, a plan, a canal, Panama!” There are a few different approaches for the test; choose one of your liking. Hint: extract all letters, change the remaining letters to lowercase, then test if the phrase is a palindrome.arrow_forward
- Write a Java class that would match a string. The class has method match with String as a return type. It accepts two inputs: the phrase/sentence string (text) and the pattern string (word). This method finds the first (or all) instances of the pattern in the text and changes that word in all uppercase and return the new phrase. Method countOccurence accepts a phrase/sentence and a pattern string and returns its number of occurrences. Add a main method that will allow the user to input the phrase/sentence and pattern. Call method match and countOccurence. See test case below. Sample Input: Text string: You will always have my love, my love, for the love I love is as lovely as love itself. Pattern string: love Output: New text: You will always have my LOVE, my LOVE, for the LOVE I LOVE is as lovely as LOVE itself. Number of occurrence: 5 For example: Input Result You will always have my love, my love, for the love I love is as lovely as love itself. love New text: You…arrow_forwardWrite a Java Program that allows the user to enter an arbitrary number of integer grades from the keyboard. Each grade should be added to an ArrayList called grades. Stop inputting grades when -1 is entered. The program should then iterate through the ArrayList, calculate the average grade, and output it along with the minimum and the maximum grade value.arrow_forwardWrite code that uses a regular expression to see if a string could be a variable, class, or method name in the java programming language (can be coded in any language).arrow_forward
- Write a Java program with two methods, main and exchange that asks to input two double variables, then prints them, and calls the exchange method to exchanges the values of these two variables. The exchange method receives two values for its parameters, prints them, then exchange values of parameters and prints their new values.arrow_forwardWrite a Java program using at least one for loop that prints pictures of piles of counters of different sizes using ‘=‘ characters as shown below. Each counter should be an odd number of characters wide and be two characters longer than the counter above it. The user should, when asked, type in the size of the top and bottom counters. For example, if the user enters 3 for the top counter and 7 for the bottom counter then the image below should be printed. your program must make use of a counter-controlled for loop be in procedural programming style (not OOP) your program should also be logically correct, solving the core problem, be elegantly written following good style, and be broken into appropriate methods that take arguments and/or return results.arrow_forwardWrite a Java program, GradeApp that reads a series of exam scores given as integers in the range 0 to 100. Display the total number of grades and the number of grades in each letter-grade category, followed class average (as a percentage with 2 decimal place): Use the value -1 as a sentinel value to indicate the end of the input. (The -1 value is used only to end the loop. Please do not use it in the calculations.)arrow_forward
- Javaarrow_forwardWrite a program using java programing language. Write a Java class (called ShowNum) that displays the same number of asterisks (row and columns) as is input in 2 numbers. Your class should have a default constructor (number1 is 0, number2 is 0) and another constructor that inputs 2 numbers (integers). You'll need accessor methods for the numbers and a display method that displays as follows: Input: 2, 7 ******* ******* Demo the class in a client (called ShowNumClient) that allows the user to run the program as many times as they want (enter number and use display method). The user will signal with a sentinel (-1), when they want to terminate the client. Make sure that each number entered in the client is between 1 and 30.arrow_forwardWrite a java program that reads a line of input as a sentence and displays:• Only the uppercase letters in the sentence.• The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strikesymbol “*”.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education