Study guide! Although it is the foundation of Java, many people have fallen into the pit

introduce

This tutorial shows you the io operation of Java, creates a basic Java program, requests the user to input information, performs some operations on the information, and then outputs the specified target display results.

In this tutorial, we will make a program to request the user's name, year of birth and current year. It will then display the user's name and age.
Our plan is a class called Tut1. The corresponding file name is * Tut1 Java, * structure is:

class Tut1{
}

Define a jvm entry method 'main'. The structure of the main method is as follows:

public static void main(String args[])
{
}

Notice the initial'S' String. This is important, otherwise you will receive an error message.

We have many different ways to output text. I'll use a method that uses standard java packages.

System.out.println("Welcome To My First Java Program ");

Again in this example, notice the capital'S' System.
This code basically prints a line of text enclosed in quotation marks. The line terminates, like most lines in java, with a semi colon.

If we put all this code together, it will look like this:

class Tut1 {
     public static void main(String args[])
     	{
          System.out.println("Welcome To My First Java Program");
     }	
}

The program will output the following on the screen:

Welcome To My First Java Program 

This is the most basic program you can provide. It doesn't provide any functions.

Next, we will extend this program to request data from users through the keyboard.
To read input from the keyboard, we will use standard Java classes. We need to use the IOException class java io.

To use this class, we must use Java Import this class from the IO package. This can be done by:

import java.io.* ;

You can import many other packages in a similar way.

The first step is to create an InputStreamReader. The format is as follows:

InputStreamReader varName = new InputStreamReader(System.in) ;

This creates a reader and assigns it to the variable varName. varName can be changed to anything you want if you follow the naming rules of variables.
This code performs the actual reading of the keyboard and converts it to Unicode characters. This is not very useful for us because we want to get the information string. This is where BufferedReader came in:

BufferedReader varName = new BufferedReader(varName) ;

Rules with variable names also apply here. Also note that you cannot have InputStreamReader and BufferedReader with the same name. I just did it for demonstration purposes. In addition, varName in parentheses, BufferedReader must be a variable name or your InputStreamReader.
The following is the actual code example we will use for the project:

InputStreamReader istream = new InputStreamReader(System.in) ;

BufferedReader bufRead = new BufferedReader(istream) ;

Note the case of characters and semicolons.

For this example, we are going to read a row. You can do this in the following ways:

String firstName = bufRead.readLine();

To read from the keyboard, you must also create a try catch block:

try {
}
catch (IOException err) {
}

To use the try catch block, you can place the code used to read the value, put any error messages in the try section and the catch block, or what to do if an error occurs.
This is the code of our project:

try {
     String firstName = bufRead.readLine();
}
catch (IOException err) {
     System.out.println("Error reading line");
}

This will attempt to read the input. It will then catch any IOException errors and print out user-friendly errors if they occur. In this case, the error is "Error reading line".
In the try block, we will also ask the user to enter their name, otherwise the user will not know what he / she should enter:

try {
     System.out.println("Please Enter In Your First Name: ");
     string firstName = bufRead.readLine();
}
catch (IOException err) {
     System.out.println("Error reading line");
}

Now let's put our code together.

import java.io.* ;
class Tut1 {
     public static void main(String args[])
     {
               InputStreamReader istream = new InputStreamReader(System.in) ;
               BufferedReader bufRead = new BufferedReader(istream) ;
               
               System.out.println("Welcome To My First Java Program");
          try {
               System.out.println("Please Enter In Your First Name: ");
               String firstName = bufRead.readLine();
          }
          catch (IOException err) {
               System.out.println("Error reading line");
          }
     }	
}

congratulations! You have now made a rather useless program. It requires information and does nothing.

We will now extend this base code.
We also want to ask the user's year of birth and current year:

System.out.println("Please Enter In The Year You Were Born: ");
String bornYear = bufRead.readLine();

System.out.println("Please Enter In The Current Year: ");
String thisYear = bufRead.readLine();

Based on this information, we want to calculate people's age. But there's a problem - how do you take a string from another person. You can't, so we have to change the string value to a numeric value. We will use the function to convert the string value to the Integer value parseInt:

int bYear = Integer.parseInt(bornYear);
int tYear = Integer.parseInt(thisYear);

This code must be in a try catch block. You can then catch conversion errors:

catch(NumberFormatException err) {
     System.out.println("Error Converting Number");
}

The next step is to calculate the person's age and output all the data:

int age = tYear – bYear ;
System.out.println("Hello " + firstName + ". You are " + age + " years old");

The last step is to combine all our code:

import java.io.* ;

class Tut1 {
     public static void main(String args[])
     {
          InputStreamReader istream = new InputStreamReader(System.in) ;
          BufferedReader bufRead = new BufferedReader(istream) ;

          System.out.println("Welcome To My First Java Program");
	
          try {
               System.out.println("Please Enter In Your First Name: ");
               String firstName = bufRead.readLine();

               System.out.println("Please Enter In The Year You Were Born: ");
               String bornYear = bufRead.readLine();

               System.out.println("Please Enter In The Current Year: ");
               String thisYear = bufRead.readLine();

               int bYear = Integer.parseInt(bornYear);
               int tYear = Integer.parseInt(thisYear);

               int age = tYear – bYear ;
               System.out.println("Hello " + firstName + ". 
				You are " + age + " years old");
          }
          catch (IOException err) {
               System.out.println("Error reading line");
          }
          catch(NumberFormatException err) {
               System.out.println("Error Converting Number");
          }			
     }
}

Perfect, our first basic Java program is completed.

Author welfare

The following is a set of "interview dictionary" prepared by Xiaobian for the upcoming golden nine silver ten, which has targeted answers to both technical and HR questions.

With this, interview stepping on thunder? It doesn't exist!

You need this "interview book", Click here to get it for free ! Give back to fans with sincerity!!!




With this, interview stepping on thunder? It doesn't exist!

You need this "interview book", Click here to get it for free ! Give back to fans with sincerity!!!

[external chain picture transferring... (img-wvmCerNQ-1623735809255)]
[external chain picture transferring... (img-wKET07tB-1623735809258)]
[external chain picture transferring... (img-rioicwro-1623735809260)]

Keywords: Java Interview Programmer

Added by taz321 on Sat, 29 Jan 2022 22:52:34 +0200