Three ways of getting data from command line in Java

Time:2021-1-13

This article mainly introduces three ways of Java to get data from the command line, code examples. The example code is introduced in great detail, which has a certain reference learning value for everyone’s study or work. Friends who need it can refer to it

The first is to use scanner object

import java.util.Scanner;

public class TestInputOuput {
  public static void main(String[] args) {
    System.out.println ("please enter");
    Scanner inp = new Scanner(System.in);
    String str = inp.next();
    System.out.println ("you entered" + STR));
  }
}

The second is to use cache stream and input stream

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestInputOuput {
  public static void main(String[] args) throws IOException {
    System.out.println ("please enter");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println (you entered:+ br.readLine ());
  }
}

Third: use input box

import javax.swing.JOptionPane;

public class TestInputOuput {
  public static void main(String[] args) {
    String jop =  JOptionPane.showInputDialog (null, please enter, input box title), JOptionPane.QUESTION_ MESSAGE);  
    System.out.println ("you entered" + jop) ";
  }
}

After running, an input box will pop up and input in.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.