Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 15th, 2005, 11:22 PM   #1
Cup O' Java
Newbie
 
Cup O' Java's Avatar
 
Join Date: Dec 2004
Posts: 18
Rep Power: 0 Cup O' Java is on a distinguished road
Homework Problem 2

Ok boys and girl here is another beauty. Now I know you probably use other methods in programs such as scanner(); and what not but im just doing it how we are doing it in class. It probably wouldnt hurt if I used other methods which could save a few lines of code but for now im just trying to get all my work done.

this program is supposed to read a text file which i have included at the bottom of this message. read each line using tokens, so it knows where each lines starts and stops. its like a book inventory system telling u if ur low or ok on books in stock.

The code:
=======
import java.io.*;
import java.util.*;
import java.lang.*;

class P2
{
public static void main(String[] args) throws IOException
line 8 =====> final int REORDER_LEVEL = 1;
FileReader file1 = new FileReader("G://S2Data.txt");
BufferedReader inputFile = new BufferedReader(file1);
FileWriter file2 = new FileWriter("G://S2Report.txt");
PrintWriter outputFile = new PrintWriter(file2);
int quantity, numberOfTokens, totalQuantity = 0;
float price, totalprice = 0.0f;
String title;
StringTokenizer data;
line 17 ====> outputFile.println(" Stock Report on Books\n");
line 18 ====> outputFile.println(" Quantity\n Price\n Title\n\n");

//tokenizer a line from the data file
line 21 =====> data = new StringTokenizer(inputFile.readLine());
line 22 =====> numberOfTokens = data.countTokens();

line 24 =====> while(numberOfTokens != 0)
{
quantity = new Integer(datanextToken()).intValue();
prince = new Float(data.nextToken()).nextValue();
title = data.nextToken("\n");
outputFile.print(quantity + "\t\t\t" + "\t" + title + "\n\r");
outputFile.flush();

if(quantity <= REORDER_LEVEL)
outputFile.println(" ----> REORDER");
else
outputFile.println();
totalQuantity += quantity;
totalPrice += price * quantity;
data = new String Tokenizer(inputFile.readLine());
numberOfTokens = data.countTokens();
}

line 42 ====> outputFile.println("\n Number of Books in Stock" + totalQuantity);
line 43 ====> outputFile.println("Value of Books in stock " + totalPrice);
line 44 ====> outputFile.close();
line 45 ====> inputFile.close();
}

the ERRORS
=========
line 9 ';' expected
line 17 <identifier> expected
line 18 <identifier> expected
line 21 <identifier> expected
line 22 <identifier> expected
line 24 illegal start of type
line 42 <identifier> expected
line 43 <identifier> expected
line 44 <identifier> expected
line 45 <identifier> expected

this is the TEXT file that its suppose to look at
========
4 15.95 The Tempest
1 18.75 Splitting the Atom
3 15.25 Hate, Lust, and Love

Now some of these errors I am getting are screwed up. i must be missing some little stupid thing which just aggravates the rest of the code.

thanks guys

Last edited by Cup O' Java; Feb 15th, 2005 at 11:30 PM.
Cup O' Java is offline   Reply With Quote
Old Feb 26th, 2005, 7:55 PM   #2
Cup O' Java
Newbie
 
Cup O' Java's Avatar
 
Join Date: Dec 2004
Posts: 18
Rep Power: 0 Cup O' Java is on a distinguished road
ok i couldnt get back into it to edit so i have to do it all over in here.
i am going to display some screenshots and show u what i have. the code COMPILES CORRECTLY so please ignore the first post. this is a more updated version, however i get a bogus dos prompt. any help?


the code
=============================
=============================
import java.io.*;
import java.util.*;
import java.text.*;

class P2
{
public static void main(String[] args) throws IOException
{
final int REORDER_LEVEL = 1;
FileReader file1 = new FileReader("G:\\java programs\\P2Data.txt");
BufferedReader inputFile = new BufferedReader(file1);
FileWriter file2 = new FileWriter("G:\\java programs\\P2Report.txt");
PrintWriter outputFile = new PrintWriter(file2);

int quantity, numberOfTokens, totalQuantity = 0;
float price, totalprice = 0f;
String title;

outputFile.println(" Stock Report on Books");
outputFile.println("-----------------------------------------------------");
outputFile.println("Quantity\t Price \t\t Title");
outputFile.println("--------\t ----- \t\t -----");
outputFile.println("");
outputFile.println("");

//tokenizer a line from the data file
StringTokenizer data = new StringTokenizer(inputFile.readLine());
numberOfTokens = data.countTokens();
NumberFormat money = NumberFormat.getCurrencyInstance();

while(numberOfTokens !=0)
{
quantity = new Integer(data.nextToken()).intValue();
price = new Float(data.nextToken()).floatValue();
title = data.nextToken("\n");
outputFile.print(" " + quantity + "\t\t" + money.format(price) + "\t\t" + title);
outputFile.flush();

if(quantity <= REORDER_LEVEL)
outputFile.println(" ----> REORDER");
else
outputFile.println();
totalQuantity += quantity;
totalprice += price * quantity;
data = new StringTokenizer(inputFile.readLine());
numberOfTokens = data.countTokens();
}
outputFile.println("");
outputFile.println("Number of Books in Stock" + totalQuantity);
outputFile.println("Value of Books in stock " + money.format(totalprice));
outputFile.close();
inputFile.close();
}
}
=========================
=========================

text file it reads from contatins this:

4 15.95 The Tempest
1 18.75 Splitting the Atom
3 15.25 Hate, Lust, and Love

==========================
==========================
dos prompt after execution (refer to photo "dosprompt"

text file it writes to contains this:

Stock Report on Books
-----------------------------------------------------
Quantity Price Title
-------- ----- -----

4 $15.95 The Tempest
1 $18.75 Splitting the Atom ----> REORDER
3 $15.25 Hate, Lust, and Love

**notice the last 2 print statements are missing from the text file that it writes to!! also this forum screwes up the format of the finished text so ignore that look at my "finishedphoto" to see how it really looks

thanks
Attached Images
File Type: jpg dosprompt.jpg (35.0 KB, 32 views)
File Type: jpg finishedphoto.jpg (35.2 KB, 27 views)

Last edited by Cup O' Java; Feb 26th, 2005 at 8:03 PM.
Cup O' Java is offline   Reply With Quote
Old Mar 1st, 2005, 10:37 AM   #3
bgbrthr133
Newbie
 
bgbrthr133's Avatar
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0 bgbrthr133 is on a distinguished road
hey i ran your prgm but i didn't get a DOS output like you said, i got the same P2Report though. i'm not sure if i fully understood your question though
"**notice the last 2 print statements are missing from the text file that it writes to!!"
was this talking about:
outputFile.println("Number of Books in Stock" + totalQuantity);
outputFile.println("Value of Books in stock " + money.format(totalprice));
?

if so it's becuase you are missing brackets around your last else statement
__________________
turn on, tune in, drop out

Last edited by bgbrthr133; Mar 1st, 2005 at 11:30 AM.
bgbrthr133 is offline   Reply With Quote
Old Mar 1st, 2005, 11:46 PM   #4
Cup O' Java
Newbie
 
Cup O' Java's Avatar
 
Join Date: Dec 2004
Posts: 18
Rep Power: 0 Cup O' Java is on a distinguished road
that wasnt the reason but thanks. that else isnt nested so its not missing any brackets.
but the way i coded this it gets screwed up with the text file that is acting as the inputFile. you see how the inputFile ends with "and Love"? well if you dont HIT the ENTER key twice there the program never reads the blank line and thinks the line hasnt ended, so it never generates the 2 last print statements and gives me those dos warnings.

thanks for everyone who helped.
Cup O' Java is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC