Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,417 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,352 people online right now. Registration is fast and FREE... Join Now!




Loops and Input/Output Files

 
Reply to this topicStart new topic

Loops and Input/Output Files

xcef2005
15 Oct, 2008 - 08:07 AM
Post #1

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Ok so Im trying to get the code to read from two files. Do alittle math and then write to a new file. My guess is it has something to do with the loop considering everything before the works. Heres the code

CODE
package lab05;
import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;

public class Main {

//Programmer:       Kevin Thayer
//Date Due:         October 14, 2008
//Project Number:   5
//Project Name:     Inventory Tracker
//Project Describition:
//      This program will be used to track inventory. The program will load the
//      inventory data from a data file and will read transactions from a
//      seperate transaction file. Once the inventory has ben updated, the
//      program should display a product order report and should rewrite the
//      orginal inventory file with the new, updated data.
    
    public static void main(String[] args)throws IOException {
        Scanner kbd = new Scanner(System.in);
        String name;
        String name2;
        double quantity;
        double price;
        double shelf;
        double reorder;
        double quantitySold;
        double orderAmount;
        String inputFileName;
        String inputFileName2;
        
        //Get File Names
        inputFileName = GetFileName();
        inputFileName2 = GetFileName();
        
        //Process Inventory Update
        //Get Inventory Item
        File inFileHandle = new File(inputFileName);
        Scanner inFile = new Scanner (inFileHandle);
        
        // Order Report Heading
        System.out.println("Product Reorder Report:");
        System.out.println("Product Name\t" + "Order Amount");
        
        while(inFile.hasNext()) {
            name = GetEntry(inFile);
            quantity = Double.parseDouble(GetEntry(inFile));
            price = Double.parseDouble(GetEntry(inFile));
            shelf = Double.parseDouble(GetEntry(inFile));
            reorder = Double.parseDouble(GetEntry(inFile));
            
           inFile.close();
          
           File inFileHandle2 = new File (inputFileName2);
           Scanner inFile2 = new Scanner (inFileHandle2);
      
           name2 = GetEntry(inFile2);
           quantitySold = Double.parseDouble(GetEntry(inFile2));
          
           inFile2.close();
          
           //Update Inventory
           quantity = quantity - quantitySold;
          
           if(quantity <= reorder){
              // How Much to Order
              orderAmount = shelf - quantity;
              
              System.out.println(name2 + "\t" + orderAmount);
           }
           FileWriter appendFile = new FileWriter("C:/Users/Kevin/Desktop/inventory2.txt", true);
           PrintWriter outFile = new PrintWriter(appendFile);
          
           outFile.println(name);
           outFile.println(quantity);
           outFile.println(price);
           outFile.println(shelf);
           outFile.println(reorder);
          
           outFile.close();
        }
        
      
    }

    public static String GetFileName() {
        String fName;
        fName = JOptionPane.showInputDialog(null, "Please enter a file name.");
        return fName;
    }

    public static String GetEntry(Scanner inFile) {
        throw new UnsupportedOperationException();
    }
      
}


Also here is the output I get from it:

init:
deps-jar:
Compiling 1 source file to C:\Users\Kevin\Documents\NetBeansProjects\Lab05\build\classes
compile:
run:
Product Reorder Report:
Product Name Order Amount
Exception in thread "main" java.lang.UnsupportedOperationException
at lab05.Main.GetEntry(Main.java:93)
at lab05.Main.main(Main.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)


Finally if this helps at all. The two files I have are called Inventory and Transactions. In the inventory file I have:
(Contains from Top to Bottum: Name, Quantity, Price, Shelf, Reorder)

20" TV
8
350.00
20
5
Ipod Touch
13
220.00
20
10
Nintendo Wii
2
250.00
15
5
DVD Movie
20
15.00
30
10
Playstion 3
5
400.00
10
5
Acer Laptop
3
600.00
5
1

In Transactions I have (From top to bottum: Name2, QuantitySold):

20" TV
3
Ipod Touch
4
Nintendo Wii
2
DVD Movie
8
Playstation 3
1
Acer Laptop
0


THANK YOU to Anyone who can help
User is offlineProfile CardPM
+Quote Post

g00se
RE: Loops And Input/Output Files
15 Oct, 2008 - 08:15 AM
Post #2

D.I.C Regular
Group Icon

Joined: 19 Sep, 2008
Posts: 455



Thanked: 38 times
My Contributions
QUOTE
Exception in thread "main" java.lang.UnsupportedOperationException


You throw this exception yourself, in GetEntry, when doing this:

CODE
name = GetEntry(inFile);


You need to close the input file only *after* reading it btw
User is offlineProfile CardPM
+Quote Post

xcef2005
RE: Loops And Input/Output Files
15 Oct, 2008 - 08:30 AM
Post #3

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Any advice what the code would look like fixed. Im still alittle confused on what to put in the method part of the GetEntry and I took away close but im wonder if theres more to the problem. Im still new to Java so I could not just be getting what your saying. Sorry
User is offlineProfile CardPM
+Quote Post

g00se
RE: Loops And Input/Output Files
15 Oct, 2008 - 09:14 AM
Post #4

D.I.C Regular
Group Icon

Joined: 19 Sep, 2008
Posts: 455



Thanked: 38 times
My Contributions
getEntry (method names should be in 'camel case' in Java) could become:

CODE

   public static String getEntry(Scanner inFile) {
        return inFile.nextLine();
    }


This post has been edited by g00se: 15 Oct, 2008 - 09:15 AM
User is offlineProfile CardPM
+Quote Post

xcef2005
RE: Loops And Input/Output Files
15 Oct, 2008 - 09:39 AM
Post #5

New D.I.C Head
*

Joined: 7 Oct, 2008
Posts: 13

Hmmm When I try that I get errors by the parseDoubles saying

"Double can not be applied to (GetEntry)"

and for the Name and Name 2 I get

"Found GetEntry
Required Java.lang.String
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:58PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month