4 Replies - 1147 Views - Last Post: 15 October 2008 - 10:39 AM Rate Topic: -----

#1 xcef2005   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 08

Loops and Input/Output Files

Posted 15 October 2008 - 09:07 AM

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: Loops and Input/Output Files

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Loops and Input/Output Files

Posted 15 October 2008 - 09:15 AM

Quote

Exception in thread "main" java.lang.UnsupportedOperationException


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

name = GetEntry(inFile);


You need to close the input file only *after* reading it btw
Was This Post Helpful? 0
  • +
  • -

#3 xcef2005   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 08

Re: Loops and Input/Output Files

Posted 15 October 2008 - 09:30 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Loops and Input/Output Files

Posted 15 October 2008 - 10:14 AM

getEntry (method names should be in 'camel case' in Java) could become:

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


This post has been edited by g00se: 15 October 2008 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 xcef2005   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 08

Re: Loops and Input/Output Files

Posted 15 October 2008 - 10:39 AM

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1