
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
- I need to make a parallel version of this sequential code.arrow_forwardBenefits of using arrays as instance variables: What are the advantages of incorporating arrays as instance variables within a class? Initializing and managing arrays: How do you initialize and manage arrays within class constructors and mutators (setters)? Example of using arrays as instance variables: Share an example where you have used arrays as instance variables and discuss its application in a real-world scenario. Common mistakes with arrays as instance variables: What are some common mistakes to avoid when working with arrays as instance variables? Information hiding violations: What is the potential violation of information hiding when using arrays as instance variables? How can this be resolved?arrow_forwardDo you think that computers should replace teachers? Give three references with your answer.arrow_forward
- Is online learning or face to face learning better to teach students around the around the world? Give reasons for your answer and provide two references with your response. What are benefits of both online learning and face to face learning ? Give two references with your answer. How does online learning and face to face learning affects students around the world? Give two references with your answer.arrow_forwardExplain Five reasons if computers should replace teachers. Provide three references with your answer. List three advantages and three disadvantages face to face learning and online learning may have on children. Provide two references with your answer.arrow_forwardYou were requested to design IP addresses for the following network using the address block 10.10.10.0/24. Specify an address and net mask for each network and router interfacearrow_forward
- For the following network, propose routing tables in each of the routers R1 to R5arrow_forwardFor the following network, propose routing tables in each of the routers R1 to R5arrow_forwardUsing R language. Here is the information link. http://www.cnachtsheim-text.csom.umn.edu/Kutner/Chapter%20%206%20Data%20Sets/CH06PR18.txtarrow_forward
- Using R languagearrow_forwardHow can I type the Java OOP code by using JOptionPane with this following code below: public static void sellCruiseTicket(Cruise[] allCruises) { //Type the code here }arrow_forwardDraw a system/level-0 diagram for this scenario: You are developing a new customer relationship management system for the BEC store, which rents out movies to customers. Customers will provide comments on new products, and request rental extensions and new products, each of which will be stored into the system and used by the manager for purchasing movies, extra copies, etc. Each month, one employee of BEC will select their favorite movie pick of that week, which will be stored in the system. The actual inventory information will be stored in the Entertainment Tracker system, and would be retrieved by this new system as and when necessary. Example of what a level-0 diagram looks like is attached.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning




