User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,234 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,810 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 940 | Replies: 13
Reply
Join Date: Nov 2007
Posts: 14
Reputation: rickster11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rickster11 rickster11 is offline Offline
Newbie Poster

Help Anyone offer any advice with small program...

  #1  
Nov 15th, 2007
I'm having all sorts of problems getting this program to work. I think I know what's wrong with it, but I don't know how to fix the problem.

The program is supposed to read a text file that contains information like below....

1 99
2 88
3 77
4 66
5 55
1 10
2 35
5 50
3 60

It supposed to add up the numbers on the right, corresponding with the numbers on the left and write that information to a file. For example there are 105 "5"s and 123 "2"s.

So the new file should look like this

1 109
2 123
3 137
4 66
5 105

I have to do the writing with a Method, and that method has to be accessed 5 times....

I've been working on this little program for 2 days now, and I really don't think the teacher explained this.

The problem is...when I open my newly created text file. All I see is the last one "5 105"

I'm guessing this is because each time the detail line method runs, it writes over the last entry (or creates a new txt file over the last one). I thought I could get read of this problem by moving the creation point of my outputfile (summaryReport) to my main, but then my method has all sorts of problems..as it doesn't recognize what summaryReport is.

Can anyone point me in the right direction.



public class SpacelySprockets
{

	public static void main(String[] args) //Main Program
	{  //Start Main


		int partNumber=0;
		int quantity=0;
		int part1Total=0;
		int part2Total=0;
		int part3Total=0;
		int part4Total=0;
		int part5Total=0;

		Keyboard kbd;
		kbd = new Keyboard ();

		InputFile sprockets;
		sprockets = new InputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\sprockets.txt");

		OutputFile summaryReport;
		summaryReport = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\summaryReport.txt");

		while (!sprockets.eof())
		{   //Start Loop

			partNumber=sprockets.readInt ();
			quantity=sprockets.readInt ();


			switch (partNumber)
			{
				case 1:		part1Total = part1Total + quantity;
							break;
				case 2:		part2Total = part2Total + quantity;
							break;
				case 3:		part3Total = part3Total + quantity;
							break;
				case 4:		part4Total = part4Total + quantity;
							break;
				case 5:		part5Total = part5Total + quantity;
							break;
				default:	System.out.println ("The file has unknown part numbers ltstd");
			}


		}   //End Loop

		detailLine (part1Total, 1);
		detailLine (part2Total, 2);
		detailLine (part3Total, 3);
		detailLine (part4Total, 4);
		detailLine (part5Total, 5);
		


	}  //End Main

	public static void detailLine (int partTotal, int partNumber)
	{
		OutputFile summaryReport;
		summaryReport = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\summaryReport.txt");

		summaryReport.writeString (partNumber+"     "+partTotal);
		summaryReport.writeEOL();
                                summaryReport.close();

	}


} //End Class
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,087
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Sensei

Re: Anyone offer any advice with small program...

  #2  
Nov 15th, 2007
You are correct - you're overwriting the file on each invocation of detailLine. To fix that, you could make the output file a class-level variable or pass a reference to it as a parameter. Either way, do not open or close the file in that method - just write the data. Close it after all of the data has been written.
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: jbarker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jbarker jbarker is offline Offline
Newbie Poster

Re: Anyone offer any advice with small program...

  #3  
Dec 4th, 2007
Originally Posted by Ezzaral View Post
You are correct - you're overwriting the file on each invocation of detailLine. To fix that, you could make the output file a class-level variable or pass a reference to it as a parameter. Either way, do not open or close the file in that method - just write the data. Close it after all of the data has been written.



How do you make the output file a class level variable and get the method to recognize the file when your only writing lines?

thanks
jeff
Reply With Quote  
Join Date: Dec 2007
Posts: 13
Reputation: striker3344 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
striker3344 striker3344 is offline Offline
Newbie Poster

Re: Anyone offer any advice with small program...

  #4  
Dec 4th, 2007
class level variables can be accessed by all methods.

OutputFile summaryReport;
try making this a class-level variable.


summaryReport = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\Spacely Sprockets\\summaryReport.txt");

umm.. i think u can put this in the main method,, not sure thou!!
Last edited by striker3344 : Dec 4th, 2007 at 5:46 pm.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,087
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Sensei

Re: Anyone offer any advice with small program...

  #5  
Dec 4th, 2007
Yes, you just move it up as indicated above. It would need to be declared static since the method that is writing to it is static and it is accessed in main. (That is not really a good way to code things, but the original poster decided to use everything statically)
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: jbarker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jbarker jbarker is offline Offline
Newbie Poster

Re: Anyone offer any advice with small program...

  #6  
Dec 4th, 2007
When I put it in main the method just below the initialzation of the variables the detailed line method gives the error cannot find symbol. There appears to be a need to reference or call the file in the detailedline method. What would be a better way.

OutputFile summaryReport;
summaryReport = new OutputFile ("G:\\CIS 1030\\Spacley\\SpacelySprockets\\summaryReport.txt");

I also have the file closing after the 5 detailed line paramaters are sent. Only detail line info is written.


This class is has a poorly written professor book and the only way to get through is pain. I bought Heads up Java and am working through the chapters as fast as possible prior to term final but the sections to make this understandable are in the 400 -550 page range.

Thanks for your responses

jeff
Last edited by jbarker : Dec 4th, 2007 at 6:51 pm.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,087
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Sensei

Re: Anyone offer any advice with small program...

  #7  
Dec 4th, 2007
Do you have a class called OutputFile? The code he posted was using some wrapper class called OutputFile - it's not a standard API class.
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: jbarker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jbarker jbarker is offline Offline
Newbie Poster

Re: Anyone offer any advice with small program...

  #8  
Dec 4th, 2007
this is the output file. We are supposed to use this written by the prof and make it work. instead of learning how to read a file onour own we have this and I about 300 pages away from this point in head up java. He condensed everything to read the file into this and it was even bugged when I got it.




[

import java.io.*;
import java.text.DecimalFormat;
public class OutputFile extends Keyboard
{
private String fn;
private OutputStreamWriter out;

public OutputFile(String fn)
{
try
{
out = new OutputStreamWriter(new FileOutputStream(fn));
} catch( Exception e )
{
System.out.println( "Can't open output file '" + fn + "'" );
Keyboard standardInput = new Keyboard();
System.out.print( "Enter new file name or press enter to end program: " );
fn = standardInput.readLine( );
if( fn.length() == 0 )
System.exit( 1 );
}
}

public void close()
{
try
{
out.close();
}catch (IOException e){error("close");}
}


public void writeEOL()
{
try
{
out.write("\n");
}catch(IOException e){error("writeInt");}
}


public void writeInt(int i)
{
try
{
String s = "";
out.write(s.valueOf(i) + " ");
}catch(IOException e){error("writeInt");}
}


public void writeDouble(double d)
{
try
{
DecimalFormat df = new DecimalFormat("0.0 ");
out.write(df.format(d));
}catch(IOException e){error("writeDouble");}
}

public void writeString(String s)
{
try
{
out.write('"' + s + '"' + " ");
}catch(IOException e){error("writeString");}
}

public void writeWord(String s)
{
try
{
out.write( s + " ");
}catch(IOException e){error("writeWord");}
}

public void writeChar(char c)
{
try
{
out.write(c + " ");
}catch(IOException e){error("writeString");}
}
}

}
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: jbarker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jbarker jbarker is offline Offline
Newbie Poster

Re: Anyone offer any advice with small program...

  #9  
Dec 4th, 2007
tried to post better


[import java.io.*;
import java.text.DecimalFormat;
public class OutputFile extends Keyboard
{
private String fn;
private OutputStreamWriter out;

public OutputFile(String fn)
{
try
{
out = new OutputStreamWriter(new FileOutputStream(fn));
} catch( Exception e )
{
System.out.println( "Can't open output file '" + fn + "'" );
Keyboard standardInput = new Keyboard();
System.out.print( "Enter new file name or press enter to end program: " );
fn = standardInput.readLine( );
if( fn.length() == 0 )
System.exit( 1 );
}
}

public void close()
{
try
{
out.close();
}catch (IOException e){error("close");}
}


public void writeEOL()
{
try
{
out.write("\n");
}catch(IOException e){error("writeInt");}
}


public void writeInt(int i)
{
try
{
String s = "";
out.write(s.valueOf(i) + " ");
}catch(IOException e){error("writeInt");}
}


public void writeDouble(double d)
{
try
{
DecimalFormat df = new DecimalFormat("0.0 ");
out.write(df.format(d));
}catch(IOException e){error("writeDouble");}
}

public void writeString(String s)
{
try
{
out.write('"' + s + '"' + " ");
}catch(IOException e){error("writeString");}
}

public void writeWord(String s)
{
try
{
out.write( s + " ");
}catch(IOException e){error("writeWord");}
}

public void writeChar(char c)
{
try
{
out.write(c + " ");
}catch(IOException e){error("writeString");}
}
}
]
Last edited by jbarker : Dec 4th, 2007 at 10:37 pm.
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,087
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 307
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Posting Sensei

Re: Anyone offer any advice with small program...

  #10  
Dec 5th, 2007
Originally Posted by jbarker View Post
When I put it in main the method just below the initialzation of the variables the detailed line method gives the error cannot find symbol. There appears to be a need to reference or call the file in the detailedline method. What would be a better way.

I'm not following your description of the problem. If you would post your code (use the [ code] tags around it please), it would certainly help.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 5:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC