import java.B102`*;
class TextAnalyser
{
public static void main (String [] args)
{
int ch, ndigits = 0, nletters = 0, nother = 0;
if (args.length != 1)
{
System.err.println ("usage: java CA filename");
return;
}
FileInputStream fis = null;
try
{
fis = new FileInputStream (args [0]);
while ((ch = fis.read ()) != -1)
if (Character.isLetter ((char) ch))
nletters++;
else
if (Character.isDigit ((char) ch))
ndigits++;
else
nother++;
System.out.println ("num letters = " + nletters);
System.out.println ("num digits = " + ndigits);
System.out.println ("num other = " + nother + "\r\n");
int total = nletters + ndigits + nother;
System.out.println ("% letters = " +
(double) (100.0 * nletters / total));
System.out.println ("% digits = " +
(double) (100.0 * ndigits / total));
System.out.println ("% other = " +
(double) (100.0 * nother / total));
}
catch (IOException e)
{
System.err.println (e);
}
finally
{
try
{
fis.close ();
}
catch (IOException e)
{
}
}
}
}
编译提示:
class TextAnalyser
{
public static void main (String [] args)
{
int ch, ndigits = 0, nletters = 0, nother = 0;
if (args.length != 1)
{
System.err.println ("usage: java CA filename");
return;
}
FileInputStream fis = null;
try
{
fis = new FileInputStream (args [0]);
while ((ch = fis.read ()) != -1)
if (Character.isLetter ((char) ch))
nletters++;
else
if (Character.isDigit ((char) ch))
ndigits++;
else
nother++;
System.out.println ("num letters = " + nletters);
System.out.println ("num digits = " + ndigits);
System.out.println ("num other = " + nother + "\r\n");
int total = nletters + ndigits + nother;
System.out.println ("% letters = " +
(double) (100.0 * nletters / total));
System.out.println ("% digits = " +
(double) (100.0 * ndigits / total));
System.out.println ("% other = " +
(double) (100.0 * nother / total));
}
catch (IOException e)
{
System.err.println (e);
}
finally
{
try
{
fis.close ();
}
catch (IOException e)
{
}
}
}
}
编译提示:
改成import java.B102.*;
编译得
java
不要用java作为自己的包名
import java.io.IOException;
这句不对,应该是import java.B102.*;另外D:\DOC\JAVA实验\lab4\TextAnalyser.java:15: 找不到符号
应该导入Java的IO包
import java.io.*;
这样就可以了。
1
import java.io.FileInputStream;
import java.io.IOException;public class TextAnalyser {
public static void main(String[] args) {
int ch, ndigits = 0, nletters = 0, nother = 0;
if (args.length != 1) {
System.err.println("usage: java CA filename");
return;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(args[0]);
while ((ch = fis.read()) != -1)
if (Character.isLetter((char) ch))
nletters++;
else if (Character.isDigit((char) ch))
ndigits++;
else
nother++;
System.out.println("num letters =" + nletters);
System.out.println("num digits =" + ndigits);
System.out.println("num other =" + nother + "\r\n");
int total = nletters + ndigits + nother;
System.out.println("% letters ="
+ (double) (100.0 * nletters / total));
System.out.println("% digits ="
+ (double) (100.0 * ndigits / total));
System.out.println("% other =" + (double) (100.0 * nother / total));
} catch (IOException e) {
System.err.println(e);
} finally {
try {
fis.close();
} catch (IOException e) {
}
}
}}
把它放入D盘 通过cmd进行编译
F:\workspance>javac TextAnalyser.javaF:\workspance>java TextAnalyser d:/a.txt打印结果
num letters =11
num digits =0
num other =6% letters =64.70588235294117
% digits =0.0
% other =35.2941176470588262种方式
菜单栏: Configure (配置) -> Options...(选项)
在Options 对话框,然后找到JDK Tools(JDK工具),
选择Run Application(运行应用程序),选中<default>(<默认>), 点击 编辑
在Parameters(参数)页面中选中prompt for main method argument(提示main方法参数)即可
这回应该知道了把。多动手试试