A quick review of Java learned in class part2

Introduction

Please read part1, or if you understand the if statement in Java for the time being. Also, when creating a file, do not leave the file name and class name as they are. If you keep the name of the function you use, you will get an error.

This time, on the premise of part1, we plan to handle non-object-oriented parts such as for statements and character input.

Loop statement

Java has for and while. It is even better if you can use the example sentences properly.

First of all, from the for statement.

java/for.java


public class for
{
    public static void main(String[] args)
    {
        System.out.println("1 to terminal~Output 5");
        
        //Int type i that can be used in for statement is defined and used for control
        for(int i = 1; i <= 5; i++) //i++Can be re-executed by adding 1 to i when looping with
        {
            System.out.println(i);
        }
    }
}

The points to note in the for statement are as follows.

--Understand and use the number of loops yourself --The delimiter is ";" instead of ","

The delimiter is a syntax, so I think it can't be helped. The point is "understand and use by yourself". For example, what if the initial value of int i in the for statement is 0? In this case, we want to output an integer from "1 to 5", so if the initial value is set to 0, 0 will also be output to the terminal. Also, if the condition of "i <= 5" is set to "i <5", 5 will not be output. As you can see, the for statement is convenient, but be aware that if you specify the wrong range, an unexpected error will occur. Although not mentioned in the notes, variables have the concept of ** scope **. This means that variables defined inside ** "{}" (called blocks) can only be used inside that block **. For example, "int i" defined in the for statement in this program causes an error when trying to use "System.out.println (i)" under the for statement outside the for statement.

Then the while statement. It's the same, but note that the judgment is slightly different.

java/while.java


public class while
{
    public static void main(String[] args)
    {
        System.out.println("1 to Terminal~Output 5");

        int i = 1; //Define an integer to be used in a while statement
        while(i <= 5) //Run all the time during this condition
        {
            System.out.println(i);
            i++; //Otherwise it will loop infinitely
        }
    }
}

The points to note in the while statement are as follows.

--Write the assignment of variables and values to control one by one --Judgment is opposite to for statement

Unlike the for statement, which allows variables to be entered at the time of condition definition, the while statement is written first. (I don't think I can do that, but I don't know.)

** The for statement is "executed until the condition of ..." **, while the ** while statement is "executed all the time" **, so the usage is slightly different from the for statement. .. As a specific example, when writing a program that lets you buy juice at a vending machine, if it is a while statement, you can easily apply a program that lets you buy juice until you run out of money. (Devil)

Summary here

――How to write and move for sentences --Variable scope --Difference between while statement and for statement


Character input

There are two ways to enter characters in Java (for example, to calculate an integer) and put them in a variable. First from the first.

java/Scanner.java


import java.util.Scanner; //Required to use Scanner

public class Scanner
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in); //Prepare a Scanner with the name "sc"
        System.out.println("Enter something");
        String str = sc.next(); //Substitute for string type str for the time being
        /*int aaaa = sc.nextInt(); //Use this description for int*/
        System.out.println("What you entered" + str);

        sc.close(); //Type a command to exit after using sc
    }
}

The function that appears here is "Scanner", which assigns the input character string to String. It can also be converted to an integer type or a real number type. The first "import" part is the part that must be written in order to call the function, without which the Scanner cannot be used. The function is "because I didn't make it myself". Some of them are prepared as the basic syntax of Java without importing, so you can code them.

There is another standard input method, so I will also describe it here.

java/BufferedReader1.java


import java.io.*; //java.Load all functions in io(It's just annoying to write more than one)

public class BufferedReader
{
    public static void main(String[] args)throws IOException //Processing to throw an exception
    {
        BufferedReader br = /*Declare "br" of BufferedReader type*/
        new BufferedReader(new InputStreamReader(System.in)); //One line is fine, but it's hard to see, so line breaks
        String str = br.readLine(); //Assign keyboard input to str as String type
        /*int aaaa = Integer.parseInt(str); //Insert and use this statement when converting to int*/

        System.out.println(str); //Output to Terminal

        br.close(); //Described just in case
    }
}

In Java, "Scanner" and "Buffered Reader" can be used, and it is troublesome to write, but Buffered Reader is faster. When using either Scanner or BufferedReader, be aware that a compile error will occur unless you first write an import statement to make it usable. Also, write a process that ends when you use it ** like "sc.close ();". Same as cleaning up in reality. Exceptions will be described at a later date. For the time being, keep it here with "this kind of thing". Both are only partially written, so please refer to the official documentation. It's all listed

Summary here

--How to use Scanner and BufferedReader --Each ending procedure

Write class and object orientation in part3

Recommended Posts

A quick review of Java learned in class part3
A quick review of Java learned in class part2
A quick review of Java learned in class
Creating a matrix class in Java Part 1
A quick explanation of the five types of static in Java
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
What is a class in Java language (3 /?)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
Creating a matrix class in Java Part 2-About matrices (linear algebra)-
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
Measure the size of a folder in Java
What I learned in Java (Part 2) What are variables?
NIO.2 review of java
Review of java Shilber
Java inner class review
NIO review of java
Cause of is not visible when calling a method of another class in java
Write a class that can be ordered in Java
Browse class objects in Kotlin (instead of Java class name.class)
Write a class in Kotlin and call it in Java
What I learned in Java (Part 3) Instruction execution statement
What I learned when building a server in Java
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
[Java] Comparator of Collection class
Find a subset in Java
Summary of Java Math class
Implementation of gzip in java
Implementation of tri-tree in Java
What I learned in Java (Part 4) Conditional branching and repetition
Activate Excel file A1 cell of each sheet in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
I tried to make a client of RESAS-API in Java
Think of a Java update strategy
Let's create a TODO application in Java 4 Implementation of posting function
Various methods of Java String class
What I have learned in Java (Part 1) Java development flow and overview
3 Implement a simple interpreter in Java
I created a PDF in Java.
I can't create a Java class with a specific name in IntelliJ
Let's create a TODO application in Java 6 Implementation of search function
[Note] What I learned in half a year from inexperienced (Java)
Let's create a TODO application in Java 8 Implementation of editing function
Sort a List of Java objects
Basic usage of java Optional Part 1
StringBuffer and StringBuilder Class in Java
Mechanism and characteristics of Collection implementation class often used in Java
A simple sample callback in Java
List of members added in Java 9
[Note] What I learned in half a year from inexperienced (Java) (3)
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
A brief description of JAVA dependencies