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

解决方案 »

  1.   

    我改了一下代码,你看看能不能行
    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
    String sum, average;
    //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(wordArray);
      JOptionPane.showMessageDialog(null, sum);
      average = "average is" +average(wordArray);
      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 (String[] wordArray)
      {
    int sum = 0;
        
    for (int s = 0; s < wordArray.length; s++)
      {
      
    sum += Integer.parseInt(wordArray[s]);
    }  
      
      return sum ;
      }  
     public static double average (String[] wordArray)
     {
     double average;
      average= sum(wordArray) / wordArray.length;
      
      return average;
     } // class Lab6Program1}
      

  2.   

    目的:文件输入和命令行参数。
    1。复制您的Lab5Program1.java文件Lab6Progam1.java。由于文件名是
    不同,更改名称之类的到Lab6Program1。
    这个程序将读取整数值从一个文件中,然后计算总和及
    的平均值。这个程序将从文档中阅读整数值,并计算Sum(总和)和平均数。请从给出的Lab5 代码中改变其队列类型,从String array 变成 interger array.重写inputFromFile方法 使得可以从args[0]中读到一个数,从每行String 类型转换成一个integer类型保存。
    创建一个Lab6 文档,写好下列的内容,保存为Lab6input.txt:
    lab6 input.TXT  123
    75
    43
    221
    325
    6
    117
    763
    83
    785
    21
    775
    425
    647输入下面命令行:c:>java Lab5Program1 lab6input.txt2.用下面的方法求和 规定:public static int sum (int[] myArray, int myArraySize);这个方法将返回所有整数的和,在一个用inputFromFile 创建的部分填充的数组中。用 JOptionPane.showMessageDialog 来表示求和的结果。3.用下面的方法求平均值 
    规定:public static int average (int[] myArray, int myArraySize);请注意:要做到这个第三个方法要可以调用sum9求和)的方法。
      

  3.   

    根据你的说明,这样看怎样?import javax.swing.*;
    import java.io.*;
    import java.util.*;public class Lab6Program1
    {
    public static void main(String[] blargs)throws FileNotFoundException
            {
    int[] array=new int[1024]; //定义一个大数组。
    //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++;
           }
    Filein.close(); inputFromFile(array,blargs[0]); //读文件,把数值存入数组array.   String sum1 = "Sum is" +sum(array,count); //求和并现实。
       JOptionPane.showMessageDialog(null, sum1);   String average1="Average is "+average(array,count); //计算平均并显示。
    JOptionPane.showMessageDialog(null, average1);
       }//end main. //求和并返回值。
    public static int sum (int[] array, int arus)
    {
    int sum =0;  
    for (int s =0; s<arus; s ++)
           {  
    sum += array[s];
    }   
           return sum ;
           }  
    //计算平均数并返回值。
      public static double average (int[] wordArray, int arus)
      {
      double average;
       average= sum(wordArray,arus)/arus;
    return average;
      }
    //读文件并将字符串转换成整型数组。
    public static void  inputFromFile(int[] array,String fileName)  
    {
    try
    {
    Scanner fileIn=new Scanner(new File(fileName));
    int i=0;
    while(fileIn.hasNextLine())
    {
    array[i++]=Integer.parseInt(fileIn.nextLine().trim());
           }
    }
    catch(FileNotFoundException fnfe)
    {
    fnfe.printStackTrace();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    } // class Lab6Program1
      

  4.   

    If you finished lab5, then it will be to figure out lab6. 
    let me show you the bone. //lab6
    import javax.swing.*;// this is for the JOptionPane(use)public class Lab6Program1 {//declare the variables: intArray;arraySum;arrayAvg;public static int inputFromFile (String filename)//need this method to input 
    {
    //code goes here, 
    // in lab5 you already did the inputFromFile method, so use it in here as well, 
    //and just need to change little bit
    }public static void main(String[] args)//the main method
    {
     inputFromFile("lab6input.txt");// read from the file arraySum = sum (intArray, intArray.length);
     arrayAvg = average(intArray, intArray.length);//then output your sum, and average statement use by JOption.showMessageDialog}//then just write your sum method and average method. 
    }//hopes that help, I just finish same lab as yours. 
      

  5.   

    Hey,buddy.Very glad for your help.// in lab5 you already did the inputFromFile method, so use it in here as well,  
    //and just need to change little bit
    So 
    how to change a little bit,if you can show that to me a little bit,thx.