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 a Java program that prompts the user for at least two input values. Then write a method that gets those input values as parameters and does some calculation/manipulation with those values. The method than should return a result of the calculation/manipulation back to the main, where it is printed. The program should prompt the user, call the method, and then print a meaningful message along with the value returned from the method.
java programming
2. Write a java program which will input an integer value from the user (Use Scanner class). Your program will have mathOper() method in addition to the main() method.The main() method will be responsible for:a. extracting the 1's digit (ones), the 10's digit (tens), and any digits larger than the 10's digit. For instance, if the input is 3562, the 1's digit would be 2, the 10's digit would be 6, and the remaining digits would be 35.b. call the mathOper() method with these 3 values,c. print the results to the screen.The mathOper() method will make the following calculations:a. Multiply the 1's digit by 7, subtract the result by 2 and then divide that by 5.b. Add 3.5 to the 10's digit, multiply the result by 4.3 and then divide that by 2.c. Subtract 5 from the remaining digits, multiply the result by 3, find the remainder after dividing that by 7, and then add 6 to the remainder value.d. Add the 3 values obtained from ‘a’, ‘b’, and ‘c’ operations.e. Return the final value.Sample output…

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