Java扫描器+;分类
好的,第一个帖子,耶! 现在我知道这个话题已经被打死了。但问题是: 编写一个程序,从文本文件中读取以空格分隔的单词 并按升序显示单词。(如果两个词相同, 仅显示一个)。从命令行传递文本文件名。 假设文本文件只包含由空格分隔的单词 现在我已经计算出了文件部分的读数。但是如何“从命令行传递文件名”?还有独特性因素 帮忙 编辑: 谢谢你们的帮助。我现在的立场是:Java扫描器+;分类,java,sorting,Java,Sorting,好的,第一个帖子,耶! 现在我知道这个话题已经被打死了。但问题是: 编写一个程序,从文本文件中读取以空格分隔的单词 并按升序显示单词。(如果两个词相同, 仅显示一个)。从命令行传递文本文件名。 假设文本文件只包含由空格分隔的单词 现在我已经计算出了文件部分的读数。但是如何“从命令行传递文件名”?还有独特性因素 帮忙 编辑: 谢谢你们的帮助。我现在的立场是: import java.io.*; import java.util.*; public class Splittext { pu
import java.io.*;
import java.util.*;
public class Splittext {
public static void main(String[] args) {
String fileName = args[0];
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(fileName)));
while (s.hasNext()) {
System.out.println(s.next());
}
} catch (FileNotFoundException fnfe) {
System.exit(0);
} finally {
if (s != null) {
s.close();
}
}
TreeSet<String> ts = new TreeSet<String>();
ts.add(s);
Iterator it = ts.iterator();
while(it.hasNext()) {
String value = (String)it.next();
System.out.println("Result :" + result);
}
}
}
import java.io.*;
导入java.util.*;
公共类拆分文本{
公共静态void main(字符串[]args){
字符串文件名=args[0];
扫描器s=null;
试一试{
s=新的扫描仪(新的BufferedReader(新的文件读取器(文件名));
而(s.hasNext()){
System.out.println(s.next());
}
}捕获(FileNotFoundException fnfe){
系统出口(0);
}最后{
如果(s!=null){
s、 close();
}
}
树集ts=新树集();
ts.add(s);
迭代器it=ts.Iterator();
while(it.hasNext()){
字符串值=(String)it.next();
System.out.println(“结果:+Result”);
}
}
}
但这会产生:没有合适的add方法(java.util.Scanner);方法java.util.TreeSet.add(java.lang.String)不适用
对不起,我没有回答你的问题!非常感谢您的帮助:)请这样做
public static void main(String args[]) {
String fileName = args[0];
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(fileName));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
像这样运行
java classname readthisfile.txt
像这样做
public static void main(String args[]) {
String fileName = args[0];
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(fileName));
while (s.hasNext()) {
System.out.println(s.next());
}
} finally {
if (s != null) {
s.close();
}
}
}
像这样运行
java classname readthisfile.txt
但是如何“从命令行传递文件名”
还有独特性因素
和排序因子
启动应用程序时,
main
函数已向其传递了String[]args
,这是您访问输入参数的地方
e、 g:
将看到示例类接收以下内容:
public static void main(String[] args) {
// args.length == 2
// args[0] = "Happy"
// args[1] = "Days"
}
启动应用程序时,
main
函数已向其传递了String[]args
,这是您访问输入参数的地方
e、 g:
将看到示例类接收以下内容:
public static void main(String[] args) {
// args.length == 2
// args[0] = "Happy"
// args[1] = "Days"
}
String filename=args[0]代码>@RobHeiser作业标记已弃用。请不要用它<代码>字符串文件名=args[0]代码>@RobHeiser作业标记已弃用。请不要用它。