Java 初学 求助

xiaogang031 2012-09-17 11:28:20
实验6:目的:文件输入和命令行参数。
1。复制您的Lab5Program1.java文件Lab6Progam1.java。由于文件名是
不同,更改名称之类的到Lab6Program1。
这个程序将读取整数值从一个文件中,然后计算总和及
的平均值。


Lab5Program1.java文件给出:请完成Lab6Progam1 任务。

import javax.swing.*;
import java.io.*;
import java.util.*;

public class Lab5Program1 {
public static void main(String[] blargs)throws FileNotFoundException {

String[]wordArray;
String isOrIsNot, inputWord;//open the file and do stuff


//step 0;make sure blarg[0]exists
if(blargs.length==0){
//if there were no arguments,exit

System.exit(-1);
}

//first,get the file from blargs[0]
Scanner Filein=new Scanner(new File(blargs[0]));
//then,count the number of lines in the file
int count=0;
while(Filein.hasNextLine()){
Filein.nextLine();
count++;
}
//now count is the number of lines in the file
Filein.close();
Filein=new Scanner(new File(blargs[0]));
wordArray=new String[count];//make the array big enough
for(int i=0;i<count;i++){ //
wordArray[i]=Filein.nextLine();
}



// This line asks the user for input by popping out a single window
// with text input
inputWord = JOptionPane.showInputDialog(null, "Enter a word in all lower case:");

// if the inputWord is contained within wordArray return true
if (wordIsThere(inputWord, wordArray))
isOrIsNot = "is"; // set to is if the word is on the list
else
isOrIsNot = "is not"; // set to is not if the word is not on the list

// Output to a JOptionPane window whether the word is on the list or not
JOptionPane.showMessageDialog(null, "The word " + inputWord + " " + isOrIsNot + " on the list.");
}

} //main

public static boolean wordIsThere(String findMe, String[] theList) {
for(int i=0;i<theList.length;i++)
{
if(findMe.equals (theList[i]))return true;
if(findMe.equals ("STOP"))System.exit(0);
}
return false;
} // wordIsThere


} // class Lab5Program1



实验要求:

1.

这个程序将从文档中阅读整数值,并计算Sum(总和)和平均数。请从给出的Lab5 代码中改变其队列类型,从String array 变成 interger array.重写inputFromFile方法 使得可以从args[0]中读到一个数,从每行String 类型转换成一个integer类型保存。






inputFromFile方法如下:
private static int inputFromFile(String filename, short[] numbers){ TextFileInput in = new TextFileInput(filename); int lengthFilled = 0; String line = in.readLine(); while ( lengthFilled < numbers.length && line != null ) { numbers[lengthFilled++] = Short.parseShort(line); line = in.readLine(); } // while if ( line != null ) { System.out.println("File contains too many numbers."); System.out.println("This program can process only " + numbers.length + " numbers."); System.exit(1); } // if in.close(); return lengthFilled; } // method inputFromFile




创建一个Lab6 文档,写好下列的内容,保存为Lab6input.txt:
lab6 input.TXT

123
75
43
221
325
6
117
763
83
785
21
775
425
647

输入下面命令行:

c:>java Lab5Program1 lab6input.txt

2.用下面的方法求和

规定:public static int sum (int[] myArray, int myArraySize);

这个方法将返回所有整数的和,在一个用inputFromFile 创建的部分填充的数组中。

用 JOptionPane.showMessageDialog 来表示求和的结果。

3.用下面的方法求平均值
规定:public static int average (int[] myArray, int myArraySize);

请注意:要做到这个第三个方法要可以调用sum(求和)的方法。


弱弱已经写好了一点,但死活出现不了求和和求平均的对话框(Use a JOptionPane.showMessageDialog to display the
sum.)哪位好心人知道的,帮助改改。
Lab 5
Aim: File input and command line arguments.
1. Copy your Lab4Program1.java file to Lab5Progam1.java. Since the file name is
different, change the name of the class to Lab5Program1.
Modify the program so that it will read from a file, and the name of the file is given as
a command line argument. For example, if the name of the input file is lab5input.txt,
you would run the program using:
c:>java Lab5Program1 lab5input.txt
As you learned from reading the tutorial, each command line argument is stored in the
String array of the main method ("args[]"). If the line above is used to run the
program, args[0] should contain the String "lab5input.txt".
In the PowerPoint lecture on Arrays (available on Blackboard) you were shown a
program that uses the TextFileInput class to read from a file. Make sure that this class
is in your current directory (the same directory as Lab5Program1.java) so the run time
JVM can find it.
2. Use an editor, such as Notepad, to create the file lab5input.txt in the same directory as
your program. You may use the words in the original array of Lab4Program1, or any
other words you want.
3. Put a method similar to the inputFromFile method you saw in the PowerPoint that will
read each word from the input file and put it into a String array.
以上已经做好了,把Lab5改成Lab6的名字,即下面做的代码。





Lab 6: Aim: File input and command line arguments.
1. Copy your Lab5Program1.java file to Lab6Progam1.java. Since the file name is
different, change the name of the class to Lab6Program1.
This program will read integer values from a file, and compute the sum and the
average. Change the String array to an integer array in Lab6Program1. Rewrite the
inputFromFile method so that it reads from the file given in args[0], converts the string
value on each line to an integer and stores it in the next cell of the array.
If the name of the input file is lab6input.txt, you would run the program using:c:>java Lab5Program1 lab6input.txt
The file lab6input.txt is available on Blackboard.
2. Write a method with the following signature:
public static int sum (int[] myArray, int myArraySize);
This method should return the sum of all the integers in the partially-filled array that
was created by inputFromFile. Use a JOptionPane.showMessageDialog to display the
sum.
3. Write a method with the following signature:
public static int average (int[] myArray, int myArraySize);
This method should return the average of all the integers in the partially-filled array
that was created by inputFromFile. Note that this method can call the method sum.
Use a JOptionPane.showMessageDialog to display the sum.
The program from Lab 4 initialized the array of words by an assignment statement.
Modify the program so that it will read from a file, and the name of the file is given as
a command line argument. For example, if the name of the input file is lab5input.txt,
you would run the program using:
c:>java Lab5Program1 lab6input.txt

这是题目附带提供的 lab6 input.TXT 包
lab input.TXT

123
75
43
221
325
6
117
763
83
785
21
775
425
647

这是我做的,不知道为什么做不对,没出现结果,哪位知道的,帮我指点指点,万分感谢。



哪位知道的,求助,谢谢!
...全文
107 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧