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
- Write a java method called switchThem that accepts two integer arrays as parameters and switches the contents of the arrays. Take into account that the arrays may be of different sizes.arrow_forwardWrite a Java method that Let (rx1,ry1) and (rx2, ry2) be the top left and bottom right corners of a rectangle, and (px1,py) and (px2,py) the two end points of a horizontal line segment. Determine if the line segment lies completely inside the rectangle, lies completely outside the rectangle, or overlaps the rectanglearrow_forwardWrite a java program that shows a square frame filled with 10 buttons labeled 1 to 10, when one of the buttons is clicked, it shows the message "you clicked the # button", # meaning the corisponding button that was clickedarrow_forward
- Write a JAVA program that prompts the user to enter the x- and y-position of a center point and a radius, using text fields. When user click on the “Draw” button, draw a circle with the given center and radius in a component.arrow_forwardWrite a Java program that takes input from the user for the lengths of three sides, s1, s2, ands3, and decides if the sides of the given lengths can form a triangle. If they can, furtherdetermine if the triangle is right, acute, or obtuse.arrow_forwardwrite a code in java that has a row of 3 and a column of 3 and prints out the even numbers but if there is an odd number it instead prints it the space blankarrow_forward
- Write a complete Java program that reads in a line of text containing exactlythree words (separated by any kind or amount of whitespace) and outputs theline with spacing corrected; that is, the output has no space before the firstword and exactly one space between each pair of adjacent words.arrow_forwardWrite a JAVA program with class name as Exam using Mutable objects to calculate the volume of rectangle whose length = 5,width= 3, height= 4. [ Hint: RectangleVolume= length*width*height]arrow_forwardWrite a Java program that asks the user to enter positive numbers, and store it into (myArray) of type int. Use any letter as a sentinel. Show the sum of all the numbers, total number of numbers and their average.arrow_forward
- Please implement in java code with screenshots of outputarrow_forwardWrite a Java program which reads a string from the user and calls a method findVowels() from the main() method which passes the string as argument and prints the number of occurrences of each vowel (i.e., a, e, i, o, and u) present in that string. Read the string from the user using the Scanner class. Sample input:The quick brown fox jumps over the lazy dogSample output:a: 1e: 3i: 1o: 4u: 2Sample input:Merry Christmas!Sample output:a: 1e: 1i: 1o: 0u: 0arrow_forwardCreate a java class called Question4, and implement the following points inside the main method:1. Reads a string from the standard input device and store it in a variable called inputStr.2. Find all the letter characters in inputStr and store them in another string variable called letterChars. Then, convert all the characters in letterChars to uppercase characters (i.e., capital letters).3. Display the contents of letterChars to the user using a message dialog box.For example, if the contents of inputStr is “q123Ab455c6d”, then the final contents of letterChars will be “QABCD”.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