[Java] How to use the hasNext function

Programming study diary

December 27, 2020 [Java] How to get / output standard input deals with standard input, but I didn't know the existence of hasNext at that time, so I will summarize it briefly today.

What is the Scanner class?

Used for standard output and reading the contents of a file. When creating an instance of the Scannner class, set System.in for standard output and a file object for reading a file.

Basic writing


Scanner variable= new Scanner(value);

What is the hasNext function

It is a process to determine whether iterative processing can still be performed when a file is read by the Scannner class. Gets the values ​​in order from the front, and returns true if the value can still be obtained, false if no more values ​​can be obtained. If you don't know how many to enter, you can't receive by specifying the number of times in the for statement, so use it in such cases.

The next function is used in combination with the hasNext function. The next function gets the values ​​in order from the front, and the hasNext function advances the place to judge whether there is a value.

Basic writing


while(Object you want to repeat.hasNext()) {
String variable=Object you want to repeat.next();
Subsequent processing;
}

Sample code

A program that reads the contents of a file


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Sample {
  public static void main(String[] args) throws FileNotFoundException {
    File file = new File("sample.txt");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNext()) {
      String str = scanner.next();
      System.out.println(str);
    }

    scanner.close();
  }
}

A program that reverses and displays characters entered from the keyboard


import java.util.Scanner;

public class Sample {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        try {
            while (s.hasNext()) {
                String value = s.next();
                System.out.println(new StringBuilder(value).reverse());
            }
        } finally {
            s.close();
        }
    }
}

References

How to use the HasNext method of Scanner Active engineers explain the hasNext function of Java's Scanner class [for beginners] Get from keyboard input! How to use Scanner class in Java [For beginners]

Recommended Posts

[Java] How to use the hasNext function
[Processing × Java] How to use the function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Java] How to use the Calendar class
[Java] How to use Map
[Java] How to use Map
[Java] How to use Thread.sleep to pause the program
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use the replace () method (Java Silver)
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use the form_with method
How to use Java HttpClient (Post)
[Java] How to use join method
How to use the wrapper class
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to add the delete function
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
How to use submit method (Java Silver)
[Rails] How to use the map method
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to install the legacy version [Java]
How to use Java Scanner class (Note)
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
To use the "java" command line tool ... How to avoid popping up
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
How to use MinIO with the same function as S3 Use docker-compose
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option