The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.

To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.


Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Java Primer
Question:1 | ISBN:9781118771334 | Edition: 6


Write a short Java method, inputAllBaseTypes, that inputs a different value of each base type from the standard input device and prints it back to the standard output device.




package java_problems_datastructures;

import java.util.Scanner;

public class AllBaseTypes {

	private static void checkAllBaseTypes() {
		// This method will ask you to input all base types of data

		Scanner input = new Scanner(;
		System.out.println("Enter a byte value: ");
		// check whether entered data is valid or not using while loop
		 * we will do same for all the input types check the next entered input using
		 *  associated method such as hasNextInt(), hasNextShort()... it only
		 * enter into the block only when while condition is true.

		while (!input.hasNextByte()) {
			System.out.println("please enter valid byte value ");;
		System.out.println("you entered byte value " + input.nextByte() + " \n");

		System.out.println("please enter short value: ");
		while (!input.hasNextShort()) {

			System.out.print("please enter valid short value: \n");;
		System.out.println("entered short is " + input.nextShort());;

		System.out.print("Enter an integer : ");
		while (!input.hasNextInt()) {;
			System.out.println("Please enter valid integer : ");
		System.out.println("Entered Integer value is : " + input.nextInt());

		System.out.print("Enter a float : ");
		while (!input.hasNextFloat()) {;
			System.out.println("Please enter valid float : ");
		System.out.println("Entered float value is " + input.nextFloat());

		System.out.print("Enter a long: ");
		while (!input.hasNextLong()) {;
			System.out.println("Please enter valid long vzlue: ");
		System.out.println("Entered long value is : " + input.nextLong());

		System.out.print("Enter a double: ");
		while (!input.hasNextDouble()) {;
			System.out.println("Please enter valid double value : ");
		System.out.println("Entered double value is  : " + input.nextDouble());

		System.out.print("Enter a boolean: ");
		while (!input.hasNextBoolean()) {;
			System.out.println("Please enter boolean value: ");
		System.out.println("Entered boolean value is : "+input.nextBoolean());

		System.out.print("Enter a character ");
		char c =;
		System.out.println("Entered character is : " + c);

    // Driver method 
	public static void main(String args[]) {

		// calling the method




0 0


Post the discussion to improve the above solution.