Programming help

Azreal

New member
Ok, im having problems with one of my programs for java class. The instructions are listed here:

PROGRAM 8

Write a program that reads an expense list from the following file and prints the dollar sum of all expenses. You must use this file exactly as listed. Turn in your .java program. Absolutely no working together.

dog $9.50
cat $12.57
rat $2.18
petFood $8.69
Tacos $6.13
Soda $7.86
Shoes $5.40
Book $14.38
Burgers $8.45
Gas $5.25
Water $11.81
Electricity $10.88
Oranges $13.25
Ketchup $13.43
Salsa $14.05
Tacos $6.55
Cookies $8.37
Chicken $3.98
Cable $10.42
Phone $13.38


--------------------------------------------------------------------------------

OUTPUT
TOTAL=$186.53


Would someone mind giving me some hints on how to do this, or what would be the best way to approach it? Heh, if ya wanna give me some code samples that woudl be helpful too!
 
I coded this myself - I got all the hint's by looking at the Java 1.4 API:


Code:
/**
 * Read Class - A Class that Read's a Text file
 * @author Saad Mahamood 
 * @version 10/10/02 
 */

import java.io.*;

public class Read  
{

    public Read()
    {
        //Default Constructor
    }


    /**
     * textFile - This method read's a text file and output's
     *            it's content's to the terminal
     *@execption - IOException 
     */
    public void textFile() throws IOException
    {
        File aFile = new File("read.txt");

        if ((!aFile.isFile()) || (!aFile.canRead()))
        {
            // Let's leave
            throw new IOException("File not found");
        }

            
        BufferedReader in = new BufferedReader(new FileReader(aFile));
        
        
        String theString = null;

        for (int i=0;i < 26 ; i++ ) 
        {
            theString = in.readLine();
            System.out.println(theString);
        	
        }


    }



	public static void main(String[] args) 
	{
		System.out.println("Reading text file");
        System.out.println("***********************");
        Read aread = new Read();
        try
        {
            aread.textFile();
        }
        catch(IOException e)
        {
            System.err.println("An exception occured!");
        }

	}
}


ss1.
 
Re: Programming help

Azreal said:
Ok, im having problems with one of my programs for java class. The instructions are listed here:

PROGRAM 8

Write a program that reads an expense list from the following file and prints the dollar sum of all expenses. You must use this file exactly as listed. Turn in your .java program. Absolutely no working together.

dog $9.50
cat $12.57
rat $2.18
petFood $8.69
Tacos $6.13
Soda $7.86
Shoes $5.40
Book $14.38
Burgers $8.45
Gas $5.25
Water $11.81
Electricity $10.88
Oranges $13.25
Ketchup $13.43
Salsa $14.05
Tacos $6.55
Cookies $8.37
Chicken $3.98
Cable $10.42
Phone $13.38


--------------------------------------------------------------------------------

OUTPUT
TOTAL=$186.53


Would someone mind giving me some hints on how to do this, or what would be the best way to approach it? Heh, if ya wanna give me some code samples that woudl be helpful too!

Since all the values you are interested in start after the $ symbol, just read in each line and take the value after the $ and convert it to a float. add all those values together.
 
I didn't read it properly...

In the for loop in the code above use StringTokenizer to get just the stuff after the dollar bit, then use parseFloat to convert the String to a float data type.




ss1.
 
Back
Top