如何制作一个接受两种类型参数的Java程序?
所以我想让我的程序能够接受两个参数:第一个是文本文件路径,第二个是整数,表示我希望看到的行数 现在,我的代码可以使用text file path参数,但无法使用为integer参数创建的第二个方法 例如,我尝试如下方式运行我的程序: 运行readtest.txt 3 它打印文本文件中的所有内容,而不是我想要的3行 任何帮助都将不胜感激如何制作一个接受两种类型参数的Java程序?,java,Java,所以我想让我的程序能够接受两个参数:第一个是文本文件路径,第二个是整数,表示我希望看到的行数 现在,我的代码可以使用text file path参数,但无法使用为integer参数创建的第二个方法 例如,我尝试如下方式运行我的程序: 运行readtest.txt 3 它打印文本文件中的所有内容,而不是我想要的3行 任何帮助都将不胜感激 import java.io.*; import java.util.*; public class Read{ public static void m
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--;
}
}
}
import java.io.*;
导入java.util.*;
公开课阅读{
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串输入=新字符串(args[0]);
字符串输入=in;
if(Character.isDigit(in.charAt(in.length()-1))){
hasNumInput(输入);
}else onlyStrInput(输入);
}
公共静态void onlyStrInput(字符串文件名)引发FileNotFoundException{
文件=新文件(文件名);
扫描仪内嵌=新扫描仪(文件);
HashMap=newHashMap();
字符串输入=”;
while(infle.hasNext()){
输入=infle.next();
如果(!map.containsKey(input))map.put(input,1);
else-map.put(输入,map.get(输入)+1);
}
int指数=1;
而(!map.isEmpty()){
int max=0;
字符串maxP=“”;
对于(Map.Entry stringIntegerEntry:Map.entrySet()){
Map.Entry=(Map.Entry)stringIntegerEntry;
String key=(String)entry.getKey();
整数值=(整数)项。getValue();
//如果相等,按字母顺序排列
如果(值==最大值){
如果(最大比较(键)>0){
最大值=最大值;
maxP=键;
}
}
如果(值>最大值){
最大值=最大值;
maxP=键;
}
}
System.out.println(index++“+maxP++”=“+map.get(maxP));
//弹出最大值
map.remove(maxP);
}
}
公共静态void hasNumInput(字符串输入)引发FileNotFoundException{
int indexSpace=in.indexOf(“”);
字符串strNum=in.substring(indexSpace+1,in.length());
int num=Integer.parseInt(strNum);
字符串文件名=in.substring(0,indexSpace);
文件=新文件(文件名);
扫描仪内嵌=新扫描仪(文件);
HashMap=newHashMap();
字符串输入=”;
while(infle.hasNext()){
输入=infle.next();
如果(!map.containsKey(input))map.put(input,1);
else-map.put(输入,map.get(输入)+1);
}
int指数=1;
而(!map.isEmpty()&&num>0){
int max=0;
字符串maxP=“”;
对于(Map.Entry stringIntegerEntry:Map.entrySet()){
Map.Entry=(Map.Entry)stringIntegerEntry;
String key=(String)entry.getKey();
整数值=(整数)项。getValue();
//如果相等,按字母顺序排列
如果(值==最大值){
如果(最大比较(键)>0){
最大值=最大值;
maxP=键;
}
}
如果(值>最大值){
最大值=最大值;
maxP=键;
}
}
System.out.println(index++“+maxP++”=“+map.get(maxP));
//弹出最大值
map.remove(maxP);
num--;
}
}
}
字符串[]args
不会传递args[0]
中的所有内容,而是必须检查数组长度,然后采取相应措施。您的hasNumInput
方法应该采用int
参数(以及当前的字符串
)。大概
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])强制转换它代码>非常感谢您!这对我来说很有意义!