Java Demo Program 2

See the notes at the bottom of the page, after the program listing...

import java.io.*;   // Import the Input and Output Stream Classes
import java.util.Vector;
import java.util.StringTokenizer;

public class JavaDemo2{

   public int getNumValue(String str)
   {
      Integer tempInt;
      int startPos;
      String numString = new String();
      String trimmedString;
      String tempStr = "this is a line with strings and a number";
    
      startPos = tempStr.length();
      numString = str.substring(startPos);
      trimmedString = numString.trim(); //trims whitespace from both ends
      
      System.out.println("Number is " + trimmedString);
      
      tempInt = Integer.valueOf(trimmedString); 
      return tempInt.intValue();
   }

   public Vector getIntVector(String str)
   {
      StringTokenizer tokens = new StringTokenizer(str);
      String tempStr;
      Vector tempVector = new Vector(tokens.countTokens());
      Integer tempInteger;

      while (tokens.hasMoreElements())
      {
         tempStr = tokens.nextToken();
         tempInteger = new Integer(tempStr);
         tempVector.addElement(tempInteger);
      }
      return tempVector;
   }

   public void printVector(Vector intVector)
   {
      int i;
      Integer tempInteger;

      for(i = 0; i < intVector.size(); i++)
      {
         tempInteger = (Integer)intVector.elementAt(i);
         System.out.print("" + tempInteger.intValue() + "  ");
      }
      System.out.println();
   }
 
   public static void main(String[] args){

      String infileName = new String("sampleFile.txt");
      String outfileName = new String("sampleFileOut.txt");
      String sample = new String("String+nulls\0\0\0\0\0");
      char sample2[] = new char[80];
      int numValue;
      boolean arrayNumbers=false;
      Vector intVector;

      sample2 = ("Another string with nulls:\0\0\0\0\0\0\0\0\0").toCharArray();
      System.out.println("String with null: " + sample + " characters");
      System.out.println("Another sample with nulls: " + 
             String.copyValueOf(sample2));
      JavaDemo2 testRun = new JavaDemo2();
      try{
         BufferedReader infile =
                new BufferedReader(new FileReader(infileName));	  
                                               // Open for reading
         System.out.println("File open for reading...");
         BufferedWriter outfile = 
                   new BufferedWriter(new FileWriter(outfileName));	
                               // Open for writing
         System.out.println("File open for writing...");
         String line;  
         while((line = infile.readLine()) != null)
         {
            System.out.println(line);	// routes output to CRT
            if (line.startsWith("this is a line with strings and a number"))
            {
               numValue = testRun.getNumValue(line);
               System.out.println("This line has the number: " + numValue);
            }
            if (arrayNumbers)
            {
               intVector = new Vector();
               intVector = testRun.getIntVector(line);
               System.out.print("Here's the array: ");
               testRun.printVector(intVector);
            } 
            if (line.startsWith("5 more numbers:")) 
               arrayNumbers = true;
            else arrayNumbers = false;
            outfile.write(line, 0, line.length());  // routes output to file
				outfile.newLine();
         }
         System.out.println("Done copying file...");
         infile.close();
         outfile.close();
         System.out.println("Files closed.");
      }
      catch(IOException e){
            System.err.println(e);
            return;
}}}

Notes about the above program: