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.Scanner;
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 InputStreamReader(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("Foobar.java")));
// declare a file output object
// declare a print stre p = new PrintStream( out );
// Connect print stream to the output stream
System.out.println( "Choose the correct option to create appropriate method(s) for each variable found\\n");
System.out.println("****Menu****");
System.out.println( "(1) Set");
System.out.println( "(2) Get");
System.out.println( "(3) Both");
System.out.println( "(4) Skip");
input = in.nextInt();
// Here BufferedInputStream 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("private") != -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.println("Set Methods Prepared.......Thank you!");
if( input == 2 )
System.out.println("Get Methods Prepared.......Thank you!");
if( input == 3 )
System.out.println("Both Set and Get Methods Prepared.......Thank you!");
if( input == 4 )
System.out.println("Nothing was Changed......Thank you!");
pw.println(se);
fis.close();
br.close();
dis.close();
pw.close();
// dispose all the resources after using them.
}//END OF TRY
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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(String 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.println("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(Date val) {
birthDate = val;
}
public Date getBirthDate() {
return birthDate;
}
public static void main(String[] args)
{
System.out.println("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>