Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Assignment with file input

807598Oct 25 2006 — edited Oct 25 2006
I'm not one to join boards for the purpose of asking a question, but it's here that I'm stumped, and if anyone could help, I'd really appreciate it. Here's the first part of the task:

"Task-
Imagine that you are an owner of a hardware store and need to keep an
inventory that can tell you what different kind of tools you have how many of
each you have on hand and cost of each one.

Now do the following:
(1) Write a program (Tool.java) that,
(1.1) Initializes (and creates) a file ?hardware.dat?
(1.2) lets you input that data concerning each tool, i.e. the
program must ask the user for input and each of the user
input is written back in the file.
The file must have the following format:

Record# Tool Name Qty Cost per item (A$)
1 Electric Sander 18 35.99
2 Hammer 128 10.00
3 Jigsaw 16 14.25
4 Lawn mower 10 79.50
5 Power saw 8 89.99
6 Screwdriver 236 4.99
7 Sledgehammer 32 19.75
8 Wrench 65 6.48
(1.3) Exception handling that must be taken into account-
(1.3.1) User must not input incorrect data
(1.3.2) Cost of the item must be a number"

My code is below. So, here's my problem.
1. I tried using "isNaN" to check if the value for "quantity" is a number or not, but it produced a "cannot find symbol method" or some such, hence why I used a try and catch thing.

The problem is, the statement in the second try thing only tests it hypothetically; i.e. it tests it, but if it IS a number, it doesn't enter a value at all. If it ISN'T a number, it properly catches the exception.

But if I do the "nextInt" after the try and catch, I'll have to enter a new line. How can I do the catch, but also enters the value if it's true? "isNaN" would be the best method... and I'll separate everything into their own modules soon.

2. I have the tool Name, Quantity, and Price. However, I'm unsure how to add the record number. The program can be run at any time to add a record once at a time, so the record number needs to reflect what you're adding.

3. In the second part, "ToolDetails" reads and displays the contents of the "hardware.dat" file which the records are written to, and it also displays the total cost of all items, i.e. "Total toolName[1] cost: toolQuantity[1]*toolPrice[1]".

4. Also in the second class "ToolDetails", there needs to be a way to select a specific record by the toolName and delete all details pertaining to it, for example, you choose "Wrench", then the program deletes the record number, quantity, and price of "Wrench".


I apologise for my choppy code, it's what I've pieced together while testing out things. Can anyone please help me or provide guidance on where to research these things? Thanks a lot.

import java.io.*;
import java.util.Scanner;
import java.lang.Exception;

public class Tool
{
	
	public static void main(String[] args)
	{
		
		PrintWriter outputStream = null;
		
		try
		{
			outputStream = new PrintWriter(new FileOutputStream("hardware.dat", true));
		}
		
		catch(FileNotFoundException error)		// The above may throw an error.
		{									// Hence, the exception catching.
			System.out.println("File hardware.dat not found.");
			System.exit(0);
		}
		
		
		
		Scanner toolInput = new Scanner(System.in);
		
		
		System.out.println("Enter tool name:");
		String toolName = toolInput.nextLine();		
		
		
		System.out.println("Enter quantity:");		

		String test = toolInput.nextInt().toString;
		try
		{
			int toolQuantity = toolInput.nextInt();
		}
		
		catch(java.util.InputMismatchException ime)
		{
			System.out.println("Not a number.");
			System.exit(0);
		}
		
		int toolQuantity = toolInput.nextInt();
				
		System.out.println("Enter price of item:");
		double toolPrice = toolInput.nextDouble();
				
		outputStream.println(toolName + "   " + toolQuantity + "   " + toolPrice);
		outputStream.close();
		
		System.out.println("\nThank you, new item \"" + toolName + "\" entered.");
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 22 2006
Added on Oct 25 2006
1 comment
149 views