![](/img/trans.png)
[英]How to write a generic method that takes two arguments of the same types in java?
[英]How to make a Java program that takes two types of arguments?
所以我想讓我的程序能夠接受兩個參數:第一個是文本文件路徑,第二個是一個整數,表示我想看到多少行。
現在,我的代碼可以使用文本文件路徑參數,但我無法使用我為整數參數創建的第二種方法。
例如,我嘗試像這樣運行我的程序:
運行 讀取 test.txt 3
它打印文本文件中的所有內容,而不是我想要的 3 行。
任何幫助,將不勝感激!
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--;
}
}
}
String[] args
不會傳遞args[0]
所有內容,相反,您必須檢查數組長度,然后采取相應措施。 您的hasNumInput
方法應該采用一個int
參數(以及當前的String
)。 就像是,
public static void hasNumInput(String in, int num) throws FileNotFoundException {
File file = new File(in);
Scanner inFile = new Scanner(file);
那么你的main
可能看起來像
public static void main(String[] args) throws FileNotFoundException {
if (args.length > 1) {
hasNumInput(args[0], Integer.parseInt(args[1]));
} else {
onlyStrInput(args[0]);
}
}
args
是一個數組。 如您所知,第一個參數是數組的第一個元素,在邏輯上使用args[0]
引用,第二個命令行參數將使用args[1]
引用,依此類推。
public class Foo
{
public static void main(String args[])
{
System.out.println("String = " + args[0]);
System.out.println("int = " + args[1]);
}
}
args
數組是 String 類型,所以如果你想要一個 int,你必須確保使用Integer.valueOf(args[1]);
來轉換它Integer.valueOf(args[1]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.