473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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>
Dec 2 '07 #1
0 1782

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
0
by: tbatwork828 | last post by:
If you were like me trying to figure out how to launch context sensitive help topic by the context id, here is the link: http://weblogs.asp.net/kencox/archive/2004/09/12/228349.aspx and if...
2
by: John Baker | last post by:
I find it highly annoying that MS Access tries to go online when I want to look at the help files. Is there a way to configure it so it just looks at my local helpfiles when I hit F1?
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.