Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
Question
Book Icon
Chapter 1, Problem 1R
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 & Answer
Check Mark
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

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write an application in java that prints the sum of cubes. Promptfor and read two integer values and print the sum of eachvalue raised to the third power.
Write 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 “*”.
Write a method in java that validates that the input from the user is of type double. Set the parameter as a scanner.

Chapter 1 Solutions

Data Structures and Algorithms in Java

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education