help! empty output file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jebbyleezer
    New Member
    • Dec 2007
    • 1

    help! empty output file

    Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty.

    Here is my source code:

    import java.io.*;
    import java.util.Scann er;
    public class project
    {
    public static void main( String args[] )
    {
    try {
    FileInputStream fis = new FileInputStream ("Foobar.java") ;
    DataInputStream dis = new DataInputStream (fis);
    BufferedReader br = new BufferedReader( new InputStreamRead er(dis));


    String line = "";
    String se = "";
    String var="";
    String t="";
    Scanner in = new Scanner( System.in );
    int input;


    // Create a new file output stream
    // connected to "myfile.txt "
    PrintWriter pw = new PrintWriter ( new BufferedWriter( new FileWriter("Foo bar.java")));

    // declare a file output object
    // declare a print stre p = new PrintStream( out );

    // Connect print stream to the output stream

    System.out.prin tln( "Choose the correct option to create appropriate method(s) for each variable found\\n");
    System.out.prin tln("****Menu** **");
    System.out.prin tln( "(1) Set");
    System.out.prin tln( "(2) Get");
    System.out.prin tln( "(3) Both");
    System.out.prin tln( "(4) Skip");

    input = in.nextInt();
    // Here BufferedInputSt ream is added for fast reading.
    // dis.available() returns 0 if the file does not have more lines.
    while ((line = br.readLine()) != null) {

    // this statement reads the line from the file and print it to
    // the console.
    line = br.readLine();
    line.trim();
    pw.write( line );
    if( line.indexOf("p rivate") != -1 )
    {
    String [] newLine = line.split(" ");


    t = newLine[7];

    var = newLine[8];
    t = t.replace( ';', ' ' );
    var = var.replace( ';', ' ' );
    switch( input )
    {

    case 1:

    se+=prepareSet( t, var );
    se+="\n";

    break;

    case 2:
    se+=prepareGet( t, var );
    se+="\n";

    break;

    case 3:
    se+=prepareSet( t, var );
    se+=prepareGet( t, var );
    se+="\n";

    break;
    case 4:


    break;
    default:
    break;

    }
    }

    }//end of while
    if( input == 1 )
    System.out.prin tln("Set Methods Prepared....... Thank you!");
    if( input == 2 )
    System.out.prin tln("Get Methods Prepared....... Thank you!");
    if( input == 3 )
    System.out.prin tln("Both Set and Get Methods Prepared....... Thank you!");
    if( input == 4 )
    System.out.prin tln("Nothing was Changed......Th ank you!");
    pw.println(se);

    fis.close();
    br.close();
    dis.close();
    pw.close();
    // dispose all the resources after using them.
    }//END OF TRY

    catch (FileNotFoundEx ception e) {
    e.printStackTra ce();
    } catch (IOException e) {
    e.printStackTra ce();
    }
    }
    public static String prepareSet( String ti, String v )
    {
    String s = "public "+ ti+" set"+v + "(" + ti + " " + v + ")\n" +
    "{\n\tthis. " +v+ "=" +v+";\n}\n";

    return s;

    }
    public static String prepareGet(Stri ng ti, String v )
    {
    String s = "Public void get"+v + "()\n" +
    "{\nreturn this." +v + ";\n}\n";
    return s;
    }
    }


    HERE IS THE ASSIGNMENT (KIND OF LONG)

    Due Date: 12/05/07 5:00PM (-10% per day late)



    Objective:

    Create a program to insert public accesors (setXX and getXX methods) into a java class.



    Your program will accept a .java source file as a command line argument, and the output should be inserted into the file. The output should be written to the same file and file name as the input file.



    Sample Input: (Foobar.java)



    public class Foobar

    {

    private int _counter;

    private String _firstName;

    private Date birthDate;

    public static void main(String[] args)

    {

    System.out.prin tln("Hello World!" + FirstName);

    }

    }



    Processing:



    You program should:

    1. Open the input file
    2. Locate the private class variables
    3. Determine the variable types and names
    4. Prompt the user to create Set/Get/Both/Skip methods for each variable found
    5. Insert all the generated methods and write the output file.



    * You solution should recognize all valid variable declarations. Example:

    private int _someVar;

    private MyDate $gradYear;

    * Assume that the input file is syntactically correct and compiles properly

    * Assume 1 variable declaration per line of code. (no declarations like “private int a,b,c;” will be given)

    * Properly handle errors and exceptions. For example, missing input file, invalid user input, etc.





    Corresponding Output (Foobar.java)



    public class Foobar

    {

    private int _counter;

    private String _firstName;

    private Date birthDate;



    /* Public Accessors */

    public void setCounter(int val) {

    _counter = val;

    }

    public int getCounter() {

    return _counter;

    }

    public void setBirthDate(Da te val) {

    birthDate = val;

    }

    public Date getBirthDate() {

    return birthDate;

    }



    public static void main(String[] args)

    {

    System.out.prin tln("Hello World!" + FirstName);

    }

    }





    Sample Run:



    C:\oop\project> javac AccesGen.java

    C:\oop\project> java AccesGen Foobar.java



    Loading Foobar.java.



    Found private int _counter.

    Generate Set, Get, Both, None [S/G/B/N] ? B



    Found private int _firstName.

    Generate Set, Get, Both, None [S/G/B/N] ? N



    Found private int birthDate.

    Generate Set, Get, Both, None [S/G/B/N] ? B



    Changes written to Foobar.java

    Done

    C:\oop\project>
Working...