Home > Java > JavaBase > body text

How to enter an array from the keyboard in java

王林
Release: 2019-12-05 13:18:05
Original
17570 people have browsed it

How to enter an array from the keyboard in java

Related knowledge description:

java.util.Scanner is a new feature of Java5. We can obtain user input through the Scanner class.

nextLine()Function:

1. Enter is the end character, which means that the nextLine() method returns all characters before entering.

2. Can get blank space.

Online video tutorial sharing: java online learning

Examples are as follows:

public class exchangeNum {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
            String str=scanner.nextLine();
            String[] Arrays=str.split(",");   //通过“,”分离
            int[] a=new int[Arrays.length];
            for(int i=0;i<a.length;i++){
                a[i]=Integer.parseInt(Arrays[i]);   //将String型转化成int型
                System.out.print(a[i]+" ");
                }
        }
    }
Copy after login

Related article tutorial recommendations:java introductory learning

The above is the detailed content of How to enter an array from the keyboard in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!