Input and Output in Java - ProgrammerTech
Language:

Search

Input and Output in Java

  • Share this:
Input and Output in Java

Java I/O tools

As a Scanner, we covered the printing command, and today we will cover how to enter or ask the user to enter a value from the keyboard.

In using the input process we will go through two stages, the first is to include the input package and the second is to create an input package object and then we only deal with this object after the package is included.

This is the first time we have included a package in Java.

Class Scanner belongs to the Java.util package and is used to read input data from the user.

To include packages we write them below the word package and since we will use the input package and always. We write the command import, which means import or include, and then we choose the package such as util.scanner.

import java.util.Scanner

Then we create an object and this object we will use in keyboard input operations, and in object-oriented programming we do not deal with the class, but rather with the derived object, we will talk about it later.

We will create an object derived from the Scanner class, and to create an object from any class, we write the name of the class and after that we create a variable, and this variable is the object that we will deal with.

package gradleproject223;
    import java.util.Scanner;
    public class JavaApplication {
        public static void main(String[] args) {
            Scanner s=new Scanner(System.in);
            int n=s.nextInt();
            System.out.println("the enter is" + " " + n);
        }
    }

At first we wrote the package and its name, then we included the package under the word package, and since we are dealing with the input package, we included it via import and included the package util.Scanner, which will enable us to deal with many commands, including input commands, the program will put a yellow line below Including the package is a warning because we wrote it and we haven't used it yet and once we use it this warning will disappear.

Then we create an object with this object using keyboard input. In object-oriented programming, we do not deal with the Scanner class, but we deal with the derived object, which we will talk about later.

To create an object from any class, we write the name of the class and then write a variable after the class, which is the object that we will deal with Scanner s=new Scanner and new is the object that we derived, meaning we took a copy through new we took a copy of the class Scanner, and wrote another package System.in And she, in turn, will prepare us for the entry process.

After that, we put a scalar variable int to store numerical values ​​inside it and s this is the object that we derived from the Scanner class. Then we put the nextInt command to determine the type of input, i.e. numeric input.

If we want to print the content of variable n, we write the print command System.out.println and tell him to print the value of n that we entered from the keyboard.

And when executing the previous program, when we type any number, for example, 8 from the keyboard, it is written to us as the enter is 8. For example, if we want to receive a text variable, we use String n =s.nextLine to receive only text variables.

Example:

Write a program that asks the user to enter the name, age and id code, and then prints the result on the screen.

package gradleproject223;
    import java.util.Scanner;
    public class JavaApplication {
        public static void main(String[] args) {
          String Name = "";
          int age = 0;
          byte id = 0;
          Scanner s=new Scanner(System.in);  
          Name=n.nextLine();
          Scanner n1=new Scanner(System.in);
          age = n1.nextInt();
          Scanner n2=new Scanner(System.in);
          id = n2.nextByte();
          System.out.println("Name = " + Name);
          System.out.println("age = " + age);
          System.out.println("id = " + id);
        }
    }

We created a text variable for Name, an age variable, age, and a hands variable, byte. All of these variables have zero values ​​in order to receive this value from the same user.

  1. For the sake of name we derived Class Scanner from object s and then said that Name receives a text value from the user using nextLine.
  2. For age we derived Class Scanner from object n1 and then said that age receives an integer value from the user using nextInt.
  3. For hands we derived Class Scanner from object n2 and then said that byte receives a byte value from the user using nextByte.

Then we went to the printing process and printed the three values ​​via System.out.println after receiving them from the user.

 

Functions used with Class Scanner in Java

The Java Scanner class provides the following methods for reading the different types of primitives:

  • The nextInt() function is used to receive an int only.
  • The nextFloat() function is only used to receive a float.
  • The nextDouble() function is only used to receive a double long float.
  • The nextByte() function is used to insert a byte, ie 1 byte of memory.
  • The nextLine() function returns the next line entered into the Scanner object.
  • The nextBoolean() function is used to receive a boolean boolean value.
  • The nextLong() function returns the value entered in the Scanner object if this value is of type long.
  • The nextShort() function is used to receive a value of type short from the user.

 

Example:

import java.util.*;
    class UserInputDemo
    {  
    public static void main(String[] args)
    {
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter first number- ");
    int a= sc.nextInt();
    System.out.print("Enter second number- ");
    int b= sc.nextInt();
    System.out.print("Enter third number- ");
    int c= sc.nextInt();
    int d=a+b+c;
    System.out.println("Total= " +d);
    }
    }

 


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com