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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

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
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning