Sample Solution from
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Chapter 1
Problem 1R
Try another sample solution
Program Plan Intro

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.

Expert Solution
Program Description Answer

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;

  }

}

Sample Output

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

Not sold yet?Try another sample solution