Java console application example

Java console application

Java console application tutorial shows how to create a Java console application. The application computes some statistics.

2.3, 3.5, 5, 6.7, 3.2, 1.2, 6.7, 7.8 4.5, 2.1, 6.6, 8.7, 3.2, 1.0, 1.2, 3

Somewhere on the disk we have this file. The file name will be a console parameter to our program.

$ tree . ├── nbactions.xml ├── pom.xml └── src ├── main │ └── java │ └── com │ └── zetcode │ ├── JavaStatsEx.java │ └── MyStatsApp.java └── test └── java

This is the project structure.

  4.0.0 com.zetcode JavaConsoleApp 1.0-SNAPSHOT jar UTF-8 1.8 1.8   commons-cli commons-cli 1.4  com.opencsv opencsv 4.1  org.apache.commons commons-math3 3.6.1  org.apache.commons commons-lang3 3.7     org.codehaus.mojo exec-maven-plugin 1.5.0 com.zetcode.JavaStatsEx false -f /home/janbodnar/tmp/data.csv       

In the pom.xml file, we define the dependencies of the application. The commons-cli artifact is for parsing command line arguments, opencsv for reading CSV data, commons-math for statistical calculations, and commons-lang3 for transforming list into an array.

The exec-maven-plugin executes Java programs from Maven. In the arguments tag, we give the application the option and the filename.

package com.zetcode; /** * Starter class for MyStats application. * * @author janbodnar */ public class JavaStatsEx < /** * Application entry point. * * @param args application command line arguments */ public static void main(String[] args) < MyStatsApp msp = new MyStatsApp(); msp.run(args); >>

JavaStatsEx is the application entry point. It creates the instance of MyStatsApp and passes it the application arguments.

package com.zetcode; import com.opencsv.CSVReader; import com.opencsv.CSVReaderBuilder; import java.io.IOException; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.math3.stat.StatUtils; /** * MyStatsApp is a simple console application which computes * basic statistics of a series of data values. The application takes * a file of data as its single argument. * * @author janbodnar */ public class MyStatsApp < /** * Runs the application * * @param args an array of String arguments to be parsed */ public void run(String[] args) < CommandLine line = parseArguments(args); if (line.hasOption("filename")) < System.out.println(line.getOptionValue("filename")); String fileName = line.getOptionValue("filename"); double[] data = readData(fileName); calculateAndPrintStats(data); >else < printAppHelp(); >> /** * Parses application arguments * * @param args * @return CommandLine which represents a list of application * arguments. */ private CommandLine parseArguments(String[] args) < Options options = getOptions(); CommandLine line = null; CommandLineParser parser = new DefaultParser(); try < line = parser.parse(options, args); >catch (ParseException ex) < System.err.println(ex); printAppHelp(); System.exit(1); >return line; > /** * Reads application data from a file * * @param fileName * @return array of double values */ private double[] readData(String fileName) < Listdata = new ArrayList(); double[] mydata = null; try (Reader reader = Files.newBufferedReader(Paths.get(fileName)); CSVReader csvReader = new CSVReaderBuilder(reader).build()) < String[] nextLine; while ((nextLine = csvReader.readNext()) != null) < for (String e : nextLine) < data.add(Double.parseDouble(e)); >> mydata = ArrayUtils.toPrimitive(data.toArray(new Double[data.size()])); > catch (IOException ex) < System.err.println(ex); System.exit(1); >return mydata; > /** * Generates application command line options * * @return application Options */ private Options getOptions() < Options options = new Options(); options.addOption("f", "filename", true, "file name to load data from"); return options; >/** * Prints application help */ private void printAppHelp() < Options options = getOptions(); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("JavaStatsEx", options, true); >/** * Calculates and prints data statistics * * @param data input data */ private void calculateAndPrintStats(double[] data) < System.out.format("Geometric mean: %f%n", StatUtils.geometricMean(data)); System.out.format("Arithmetic mean: %f%n", StatUtils.mean(data)); System.out.format("Max: %f%n", StatUtils.max(data)); System.out.format("Min: %f%n", StatUtils.min(data)); System.out.format("Sum: %f%n", StatUtils.sum(data)); System.out.format("Variance: %f%n", StatUtils.variance(data)); >>
CommandLine line = parseArguments(args);

The parseArguments method parses the command line arguments. It returns CommandLine , which represents a list of arguments parsed against a Options descriptor.

Читайте также:  Event Calendar

if (line.hasOption(«filename»)) < System.out.println(line.getOptionValue("filename")); String fileName = line.getOptionValue("filename"); double[] data = readData(fileName); calculateAndPrintStats(data); >else

The application has a mandatory filename option, which points to the file to be read and compute statistics from. If it is not present, we provide the application help message.

Options options = getOptions();

The getOptions method returns the options of the application.

CommandLineParser parser = new DefaultParser(); try < line = parser.parse(options, args); >catch (ParseException ex) < System.err.println(ex); printAppHelp(); System.exit(1); >return line;

CommandLineParser parses the command line arguments. The application exits if there is a ParseException . The parser returns parsed arguments in CommandLine object.

try (Reader reader = Files.newBufferedReader(Paths.get(fileName)); CSVReader csvReader = new CSVReaderBuilder(reader).build()) 

CSVReader is used to read CSV data.

String[] nextLine; while ((nextLine = csvReader.readNext()) != null) < for (String e : nextLine) < data.add(Double.parseDouble(e)); >>

In this while loop, we read the CSV file line by line and parse the data into a list of Double values.

mydata = ArrayUtils.toPrimitive(data.toArray(new Double[data.size()]));

We need primitive data types to calculate the statistics; therefore, we transform the list into an array of primitive double values. ArrayUtils comes from the Apache Commons Lang library.

private Options getOptions()

The getOptions provides the application options.

private void printAppHelp()

The printAppHelp prints the help of the application. It uses HelpFormatter to do the job.

private void calculateAndPrintStats(double[] data)

With StatUtils , we compute some statistics. The StatUtils takes a Java array as a parameter.

$ mvn -q exec:java /home/janbodnar/tmp/data.csv Geometric mean: 3.412562 Arithmetic mean: 4.168750 Max: 8.700000 Min: 1.000000 Sum: 66.700000 Variance: 6.158292

In this tutorial, we have created a simple Java console application, which computes basic statistics from a CSV file.

Источник

Java console application

Java console application tutorial shows how to create a Java console application. The application computes some statistics.

Java console example

The following example creates a Java console application which reads data from a CSV file and computes some basic statistics.

The example uses Apache Commons libraries to parse command line arguments, do math, and transform data. The OpenCSV library is used to read CSV data.

2.3, 3.5, 5, 6.7, 3.2, 1.2, 6.7, 7.8 4.5, 2.1, 6.6, 8.7, 3.2, 1.0, 1.2, 3

This is the data file. The file name will be a console parameter to our program.

pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ JavaStatsEx.java │ │ MyStatsApp.java │ └───resources │ data.txt └───test └───java

This is the project structure.

  4.0.0 com.zetcode javastatsex 1.0-SNAPSHOT UTF-8 12 12   commons-cli commons-cli 1.4  com.opencsv opencsv 4.6  org.apache.commons commons-math3 3.6.1    

In the pom.xml file, we define the dependencies of the application. The commons-cli artifact is for parsing command line arguments, opencsv for reading CSV data, commons-math for statistical calculations, and commons-lang3 for transforming list into an array.

The exec-maven-plugin executes Java programs from Maven. In the arguments tag, we give the application the option and the filename.

package com.zetcode; /** * Starter class for MyStats application. * * @author janbodnar */ public class JavaStatsEx < /** * Application entry point. * * @param args application command line arguments */ public static void main(String[] args) < var app = new MyStatsApp(); app.run(args); >>

JavaStatsEx is the application entry point. It creates the instance of MyStatsApp and passes it the application arguments.

package com.zetcode; import com.opencsv.CSVReaderBuilder; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.math3.stat.StatUtils; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; /** * MyStatsApp is a simple console application which computes * basic statistics of a series of data values. The application takes * a file of data as its single argument. * * @author janbodnar */ public class MyStatsApp < /** * Runs the application * * @param args an array of String arguments to be parsed */ public void run(String[] args) < CommandLine line = parseArguments(args); if (line.hasOption("filename")) < System.out.println(line.getOptionValue("filename")); String fileName = line.getOptionValue("filename"); double[] data = readData(fileName); calculateAndPrintStats(data); >else < printAppHelp(); >> /** * Parses application arguments * * @param args application arguments * @return CommandLine which represents a list of application * arguments. */ private CommandLine parseArguments(String[] args) < Options options = getOptions(); CommandLine line = null; CommandLineParser parser = new DefaultParser(); try < line = parser.parse(options, args); >catch (ParseException ex) < System.err.println("Failed to parse command line arguments"); System.err.println(ex.toString()); printAppHelp(); System.exit(1); >return line; > /** * Reads application data from a file * * @param fileName file of application data * @return array of double values */ private double[] readData(String fileName) < var data = new ArrayList(); double[] mydata = null; try (var reader = Files.newBufferedReader(Paths.get(fileName)); var csvReader = new CSVReaderBuilder(reader).build()) < String[] nextLine; while ((nextLine = csvReader.readNext()) != null) < for (String e : nextLine) < data.add(Double.parseDouble(e)); >> mydata = ArrayUtils.toPrimitive(data.toArray(new Double[0])); > catch (IOException ex) < System.err.println("Failed to read file"); System.err.println(ex.toString()); System.exit(1); >return mydata; > /** * Generates application command line options * * @return application Options */ private Options getOptions() < var options = new Options(); options.addOption("f", "filename", true, "file name to load data from"); return options; >/** * Prints application help */ private void printAppHelp() < Options options = getOptions(); var formatter = new HelpFormatter(); formatter.printHelp("JavaStatsEx", options, true); >/** * Calculates and prints data statistics * * @param data input data */ private void calculateAndPrintStats(double[] data) < System.out.format("Geometric mean: %f%n", StatUtils.geometricMean(data)); System.out.format("Arithmetic mean: %f%n", StatUtils.mean(data)); System.out.format("Max: %f%n", StatUtils.max(data)); System.out.format("Min: %f%n", StatUtils.min(data)); System.out.format("Sum: %f%n", StatUtils.sum(data)); System.out.format("Variance: %f%n", StatUtils.variance(data)); >>
CommandLine line = parseArguments(args);

The parseArguments method parses the command line arguments. It returns CommandLine , which represents a list of arguments parsed against a Options descriptor.

if (line.hasOption("filename")) < System.out.println(line.getOptionValue("filename")); String fileName = line.getOptionValue("filename"); double[] data = readData(fileName); calculateAndPrintStats(data); >else

The application has a mandatory filename option, which points to the file to be read and compute statistics from. If it is not present, we provide the application help message.

Options options = getOptions();

The getOptions method returns the options of the application.

try < line = parser.parse(options, args); >catch (ParseException ex) < System.err.println("Failed to parse command line arguments"); System.err.println(ex.toString()); printAppHelp(); System.exit(1); >return line;

CommandLineParser parses the command line arguments. The application exits if there is a ParseException . The parser returns parsed arguments in CommandLine object.

try (var reader = Files.newBufferedReader(Paths.get(fileName)); var csvReader = new CSVReaderBuilder(reader).build()) 

CSVReader is used to read CSV data.

String[] nextLine; while ((nextLine = csvReader.readNext()) != null) < for (String e : nextLine) < data.add(Double.parseDouble(e)); >>

In this while loop, we read the CSV file line by line and parse the data into a list of Double values.

mydata = ArrayUtils.toPrimitive(data.toArray(new Double[0]));

We need primitive data types to calculate the statistics; therefore, we transform the list into an array of primitive double values. ArrayUtils comes from the Apache Commons Lang library.

private Options getOptions()

The getOptions provides the application options.

private void printAppHelp()

The printAppHelp prints the help of the application. It uses HelpFormatter to do the job.

private void calculateAndPrintStats(double[] data)

With StatUtils , we compute some statistics. The StatUtils takes a Java array as a parameter.

src/main/resources/data.txt Geometric mean: 3.412562 Arithmetic mean: 4.168750 Max: 8.700000 Min: 1.000000 Sum: 66.700000 Variance: 6.158292

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

In this article we have created a simple Java console application, which computes basic statistics from a CSV file.

Источник

Оцените статью