Input and output java 1
Input and output java 1
Objectives:
To understand the basic concepts of input and output in Java.
To learn how to use the Scanner class to capture user input.
To implement and utilize the System.out.print and System.out.println methods for
displaying output.
To develop a simple Java program that interacts with the user by capturing input and
providing output.
Introduction:
Java: A highly common of high-level, object-oriented programming language intended for
soup-to-nuts applications that are specifically constructed to work well regardless where we
run. Java. One of the advantages of Java is that if our code compiles on one system it will
run anywhere due to its JVM. Input and Output (stream)One of the basic functionalities
which makes Java code full-fledged, input/output operations for communicating with users or
interacting between systems.
Print() Method:
The System. out. print and System. out. In Java, println methods are indispensable for
printing the information in console. Print() method will print the text in same line without a
newline at the end of it, while println It adds an extra space or new line after printing where
cursor moves to next nline. These methods are essentials to show messages, prompts and
outcomes with user.
Scanner Class:
The java.util.Scanner is a simple text scanner which can parse primitive types and strings
using regular expressions. User input in Java is one of the most flexible mechanisms for
reading data from a console by using Scanner library, which belong to java. util package.
Including nextInt() for integers, nextLine() for string or +nextInt(), nextDouble() for decimal
numbers. These classes read input from various sources, such as your keyboard,… This
means you can create interactive programs that respond to user's inputs.
Implement Code:
Println()Output:
package basic;
public class Print {
public static void main(String[] args) {
String name ="Rayhan Haidar";
int id= 4;
String home= "Saidpur";
String scl="Sunflower School";
String blood="B+";
double cg=3.79;
System.out.println("My name is:"+name);
System.out.println("ID:"+id);
System.out.println("My school name is:"+scl);
System.out.println("My blood is:"+blood);
System.out.println("My CGPA is:"+cg);
}
}
Output:
Scanner Input:
package javainput;
import java.util.*;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("Enter a integer number:");
int a = sc.nextInt();
System.out.println("The integer input is:"+a);
System.out.println("Enter a Float number:");
float b= sc.nextFloat();
System.out.println("The float input is:"+b);
System.out.println("Enter a Character:");
char ch= sc.next().charAt(0);
System.out.println("The character input is: "+ch);
}
}
Output:
Conclusion:
This time we explored inputs and outputs in Java using System.out.print, System.out.println,
and the Scanner class. These are important tools for managing user interactions in general
Java programs, allowing us to capture input as well as output the results of our processing in
a neat way. These basic concepts are very important as they form the foundation for all other
advanced programming tasks in Java.