Skip to main content
Advertisement

2.6 Console Input and Output (I/O)

Until now, we have actively verified information purely by manually assigning literal values directly to variables and unconditionally printing them to the screen. In this chapter, we explore how to dynamically print sentences strictly adhering to precise formats and conversely, how to aggressively capture and dynamically store values actively inputted by a user via the keyboard.

1. Formatted Output: printf()

Generally, developers frequently utilize System.out.print() or the automatic line-breaking println() to output sentences. However, if you natively desire to cleanly align and drastically organize the strict format of the outputted metrics, you universally utilize printf().

Within printf, Format Specifiers are fundamentally utilized, firmly incorporating the % symbol to aggressively designate precisely what type of dynamic data belongs precisely at which categorical position securely.

// Central Format Specifier Index
// %d : Decimal Integer (Base-10)
// %f : Floating-point Real Number
// %s : String value
// %c : Character value

int age = 20;
double height = 175.5;
String name = "Alice";

// printf standard usage (Please observe that line breaks are NOT automatic, necessitating \n securely appended backwards)
System.out.printf("Hello, my specific name is %s and I am exactly %d years old.\n", name, age);
// Output: Hello, my specific name is Alice and I am exactly 20 years old.

// Structurally standardizing the precision decimals of a float (%.2f mathematically securely rounds up strictly to essentially the second decimal precision securely)
System.out.printf("Her absolute height is perfectly %.1fcm strictly.\n", height);
// Output: Her absolute height is perfectly 175.5cm strictly.

2. Receiving Keyboard Input: Scanner

In Java, the absolute most universal and aggressively convenient native class thoroughly utilized natively to rigorously fetch live values directly inputted sequentially from the terminal screen is inherently the Scanner.

To structurally execute the Scanner class flawlessly, you must aggressively inform the Java Compiler structurally that you actively wish to effectively utilize this precise package explicitly. Consequently, you absolutely must strictly declare import java.util.Scanner; accurately at the top of your native code.

import java.util.Scanner; // (1) Proactively import the generic Scanner Class

public class ScannerEx {
public static void main(String[] args) {
// (2) Dynamically generate the Scanner object natively (System.in rigidly dictates genuine standard keyboard input)
Scanner scanner = new Scanner(System.in);

System.out.print("Please cleanly input your absolute name: ");
String name = scanner.nextLine(); // (3) Confidently read and inherently capture the full complete input line fundamentally as a native String

System.out.print("Please flawlessly specify your categorical age explicitly: ");
int age = scanner.nextInt(); // (4) Aggressively structurally fetch precisely numerical digits securely

System.out.printf("Tremendous absolute welcome! %s, it thoroughly appears you are %d immensely!\n", name, age);

// (5) Exhausted scanners should be flawlessly closed structurally for pristine memory management natively.
scanner.close();
}
}

💡 Contrasting Core Core Input Methods

Crucially predicting precisely what structural native data type the unpredictable user actively types aggressively is absolutely completely necessary to explicitly trigger the exact compatible methods flawlessly natively preventing disastrous unexpected execution native errors.

  • nextLine() : Radically captures securely the entirely unbroken full sequence identically functioning as an absolute unbroken structural 'String line' essentially including any raw whitespace. (Gracefully structurally implicitly terminates exactly upon detecting 'Enter' Native execution)
  • next() : Aggressively exclusively strictly grabs categorically essentially exactly a solitary localized 'word' absolutely explicitly interpreting whitespace (Space) thoroughly as physical strict structural termination precisely.
  • nextInt() : Dynamically essentially catches strictly exactly native numerical sequential figures proactively thoroughly flawlessly transforming them securely flawlessly exactly directly physically into native int variables seamlessly natively.
  • nextDouble() : Analogously captures flawlessly essentially explicit localized numerical metric digits exactly transforming them flawlessly effectively flawlessly fully natively into resilient double floating-point structurally gracefully.
Advertisement