How to read data into--java language from standard input

Source: Internet
Author: User

Enter the basic questions to consider

The algorithm competition topic usually has many test cases, uses the Redirect method to send the data to the program.
Then observe whether the output of the program is consistent with the expected results.
In most cases, these tests are present in the form of a file. This should be noted in the following questions:

    1. The data for each row has multiple items, and the delimiter is not fully determined, such as: one or more spaces.
    2. The number of data on each row may not be known. Continued until the end of the bank.
    3. A total number of lines may not be known, until the end of the file.
    4. The last line may not have a carriage return line break and will encounter EOF (file end tag) directly
    5. There may be different ways to line up under different operating systems.
    6. When a string is provided, it may contain spaces.

Let's cite some of the most common examples.

Several lines, one integer per line

Measured data

35-79
import java.util.*;import java.io.*;publicclass A{    publicstaticvoidmain(String[] args){        new Scanner(System.in);                try{            while(true){                System.out.println("-> " + scan.nextInt());            }         }        catch(Exception e){}    }}

In this way, it doesn't matter if the last line has a newline character.
Even the last number of empty lines is not a problem.

Several lines, two integers per line, separated by one or more spaces

Test data

10 2030   4050 60
import java.util.*;import java.io.*;publicclass B{    publicstaticvoidmain(String[] args){        new Scanner(System.in);                try{            while(true){                System.out.println("-> " + scan.nextInt"," + scan.nextInt());            }         }        catch(Exception e){}    }}

How to read data into--java language from standard input

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.