gpt4 book ai didi

java - 从命令行获取参数

转载 作者:行者123 更新时间:2023-11-29 05:32:37 25 4
gpt4 key购买 nike

我编写了以下代码,用于将几行代码扫描为字符串并将其解析为整数。但是在我的主要方法中,我使用 p.letsReadIn 来读入文件。有谁知道我如何将其转换为使用命令行参数而不是接收文件?因此,与其让用户更改 p.letsReadIn("ReadMuah.txt");每次,他们都可以使用命令行来读取文件。

public void letsReadIn(String filename) throws FileNotFoundException {
int x;
Scanner scan = new Scanner(new File(filename));

StringTokenizer st;
String nextLine = scan.nextLine();

st = new StringTokenizer(nextLine);

x = Integer.parseInt(st.nextToken());

nextLine = scan.nextLine();

st = new StringTokenizer(nextLine);

y= Integer.parseInt(st.nextToken());

nextLine = scan.nextLine();

st = new StringTokenizer(nextLine);

z= Integer.parseInt(st.nextToken());

for (int i = 0; i < x; i++) {
nextLine = scan.nextLine();
st = new StringTokenizer(nextLine);
listing.add(Integer.parseInt(st.nextToken()));
}

scan.close();
}
public static void main(String[] args) throws FileNotFoundException {

BagItems p = new BagItems();
p.letsReadIn("ReadMuah.txt");
p.StartBagging();

}

最佳答案

main() 方法的String[] args 参数是命令行参数。如果您将文件名放在命令行中,它将位于该数组中。

关于java - 从命令行获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com