How to make a Java program that takes two types of arguments?

S H :

so I want to have my program able to take two arguments: the first one is a text file path and the second one is an integer represents how many lines I want to see.

Right now, my code can work with the text file path argument but I can't get to the second method I made for the integer argument.

For example, I try to run my program like this:

run Read test.txt 3

And it prints everything in the text file instead of 3 lines which I want it to be.

Any help would be appreciated!

import java.io.*;
import java.util.*;


public class Read{
  public static void main(String[] args) throws FileNotFoundException{
    String in = new String(args[0]);
    String input = in;
    if (Character.isDigit(in.charAt(in.length() - 1))){
      hasNumInput(input);
    }else onlyStrInput(input);
  }

  public static void onlyStrInput(String filename) throws FileNotFoundException{
    File file = new File(filename);
    Scanner inFile = new Scanner(file);
    HashMap<String, Integer> map = new HashMap<>();
    String input = "";
    while (inFile.hasNext()){
      input = inFile.next();
      if (!map.containsKey(input)) map.put(input,1);
      else map.put(input, map.get(input) + 1);
    }

    int index = 1;
    while (!map.isEmpty()) {
      int max = 0;
      String maxP = "";
      for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
        Map.Entry entry = (Map.Entry) stringIntegerEntry;
        String key = (String) entry.getKey();
        Integer value = (Integer) entry.getValue();
        //if equals, alphabetically
        if (value == max) {
          if (maxP.compareTo(key) > 0) {
            max = value;
            maxP = key;
          }
        }
        if (value > max) {
          max = value;
          maxP = key;
        }
      }
      System.out.println(index++ + ". " + maxP + " = " + map.get(maxP));
      //pop out the max
      map.remove(maxP);
    }
  }

  public static void hasNumInput(String in) throws FileNotFoundException{
      int indexSpace = in.indexOf(" ");
      String strNum = in.substring(indexSpace + 1,in.length());
      int num = Integer.parseInt(strNum);
      String filename = in.substring(0,indexSpace);

      File file = new File(filename);
      Scanner inFile = new Scanner(file);
      HashMap<String, Integer> map = new HashMap<>();
      String input = "";
      while (inFile.hasNext()){
        input = inFile.next();
        if (!map.containsKey(input)) map.put(input,1);
        else map.put(input, map.get(input) + 1);
      }

      int index = 1;
      while (!map.isEmpty() && num > 0) {
        int max = 0;
        String maxP = "";
        for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
          Map.Entry entry = (Map.Entry) stringIntegerEntry;
          String key = (String) entry.getKey();
          Integer value = (Integer) entry.getValue();
          //if equals, alphabetically
          if (value == max) {
            if (maxP.compareTo(key) > 0) {
              max = value;
              maxP = key;
            }
          }
          if (value > max) {
            max = value;
            maxP = key;
          }
        }
        System.out.println(index++ + ". " + maxP + " = " + map.get(maxP));
        //pop out the max
        map.remove(maxP);
        num--;
      }
  }
}
Elliott Frisch :

String[] args does not pass everything in args[0], instead you must check the array length and then act accordingly. Your hasNumInput method should take an int argument (and the current String). Something like,

public static void hasNumInput(String in, int num) throws FileNotFoundException {
    File file = new File(in);
    Scanner inFile = new Scanner(file);

Then your main might look like

public static void main(String[] args) throws FileNotFoundException {
    if (args.length > 1) {
        hasNumInput(args[0], Integer.parseInt(args[1]));
    } else {
        onlyStrInput(args[0]);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358922&siteId=1