Home >Java >JavaBase >How to enter an array from the keyboard in java

How to enter an array from the keyboard in java

王林
王林Original
2019-12-05 13:18:0517684browse

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]+" ");
                }
        }
    }

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!

Statement:
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