![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2004
Posts: 18
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Dec 2004
Posts: 18
Rep Power: 0
![]() |
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 Last edited by Cup O' Java; Feb 26th, 2005 at 8:03 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Dec 2004
Posts: 18
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|