Advertisement
StormWingDelta

Scanning For A File Issue

Apr 14th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class ReadANDWright
  5. {
  6.     public static void main(String args[]) throws IOException
  7.     {
  8.        
  9.         // C:\\Documents and Settings\\346778\\My Documents\\TextFiles\\StudentScores.txt
  10.         // C:\\Documents and Settings\\Roland.USER-6FEF0AE329\\My Documents\\JavaProgramsMade\\JavaLessons\\TextFiles\\StudentScores.txt
  11.     // I want to make these long file paths far shorter than they are so I don't have to type them in each time and I can save and load from at least one file/directory above what the classes are in.
  12.         Scanner sf = new Scanner(new File("C:\\Documents and Settings\\Roland.USER-6FEF0AE329\\My Documents\\JavaProgramsMade\\JavaLessons\\TextFiles\\StudentScores.txt"));
  13.         int maxIndx = -1; //-1 so when we increment below, the first index is 0
  14.         String text[] = new String[1000]; //To be safe, declare more than we need
  15.         while(sf.hasNext( ))
  16.         {
  17.             maxIndx++;
  18.             text[maxIndx] = sf.nextLine( );
  19.             //System.out.println(text[maxIndx]); //Remove rem for testing
  20.         }
  21.         //maxIndx is now the highest index of text[], -1 if no text lines
  22.         sf.close( ); //We opened a file above, so close it when finished.
  23.        
  24.         FileWriter fw = new FileWriter("C:\\Documents and Settings\\Roland.USER-6FEF0AE329\\My Documents\\JavaProgramsMade\\JavaLessons\\TextFiles\\StudentScores2.txt",true);
  25.         PrintWriter output = new PrintWriter(fw);
  26.         String answer = ""; //We will accumulate the answer string here.
  27.         int sum, FF; //accumulates sum of integers
  28.         int Div;
  29.         for(int j =0; j <= maxIndx; j++)
  30.         {
  31.             Scanner sc = new Scanner(text[j]);
  32.             sum = 0; //important to set to 0; otherwise it will remember the last sum
  33.             FF = 0;
  34.             Div = 0;
  35.             answer = ""; //otherwise it will remember last answer String
  36.             answer = sc.next();
  37.             while( sc.hasNextInt( ) ) //We could also have used hasNextInt( ) here
  38.             {
  39.                 int i = sc.nextInt( );
  40.                 FF++;
  41.                 sum = sum + i;
  42.                
  43.             }
  44.             Div = (int)Math.round((double)sum / (double)FF);
  45.             //answer = answer + " = " + sum + " / " + FF + " = " + Div;
  46.             answer = answer + ", " + "average = " + Div;
  47.             output.println(answer);
  48.             //System.out.println(answer);
  49.         }
  50.         output.close( ); //These two lines are very important. Some of the data
  51.         fw.close( );//may not actually be put on disk until you close.
  52.     }
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement