The input of an indefinite length array is ended by a return key.

Source: Internet
Author: User

Problem Description:

Input Sample: 1 2 3 4 5 6

Output Sample: 1 2 3 4 5 6

Variable length, the data is entered with a space interval and the ENTER key is ended.

Solve the misunderstanding:

To solve the problem, have been thinking about the scanner class to get to ' \ n ' to jump out of the input loop

With several attempts, it is found that the next () method does not capture what you want to get.

Post-Program:

To end the entry of a string of data with the ENTER key, all data that needs to be entered must be one line, and the nextline () function can be used to obtain all.

See the following code:

Package blogtext;import java.util.arraylist;import java.util.scanner;public class Enter {    public static void main (string[] args) {        Scanner scanner=new Scanner (system.in); Arraylist<integer> arraylist=new arraylist<>(); String value=scanner.nextline (); String []nums=value.split (""); for (int i=0;i<nums.length;i++) {int temp=integer.parseint (Nums[i]), Arraylist.add (temp);} for (int i=0;i <arraylist.size (); i++) {System.out.println (Arraylist.get (i));}} }

The input of an indefinite length array is ended by a return key.

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.