Java初学求助
Java初学求助 要求编一个程序,可以读取Lab6input TXT文档,对文档中数字求和和求平均。弱弱已经写好了一点,但死活出现不了求和和求平均的对话框(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
这是我做的,不知道为什么做不对,没出现结果,哪位知道的,帮我指点指点,万分感谢。
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Lab6Program1 {
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.");
sum = "Sum is" +sum;
JOptionPane.showMessageDialog(null, sum);
average = "average is" +average;
JOptionPane.showMessageDialog(null, average);
}
} //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
private int[] numbers;
public static int sum (int[] wordArray, int arus)
{
int sum =0;
for (int s =0; s<arus; s ++)
{
sum += numbers[s];
}
return sum ;
}
public static int average (int[] wordArray, int arus)
{
double average;
average= sum/arus;
}
return average;
} // class Lab6Program1