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以下是我自己写的代码?
大家帮我看看, 哪里有错好吗?import java.io.*;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class Tool {
public static void main (String [] args)
throws IOException {String RecordNumber; 
String name;
String quantity;
String cost; // create new buffered output writer
PrintWriter pw = new PrintWriter (new 
BufferedWriter (new FileWriter ("hardware.dat")));

Scanner console = new Scanner (System.in); System.out.print("Enter the RecordNumber");
RecordNumber = console.nextLine();


System.out.println("Enter Tool Name");

name = console.nextLine ();
System.out.println("Enter Quantity");

quantity = console.nextLine ();
System.out.println("Cost per item(A$)");

cost = console.nextLine();
pw.println(""+name+"\t"+quantity+"\t"+cost);
System.out.println("Continue Y/N ?"); }  while  (console.nextLine ().charAt(0) == 'Y');
pw.close();
}
}

解决方案 »

  1.   

    1. 创建 hardware.dat
    1.2 用户把下列信息输入到hardware.dat
    1.3异常处理
    1.3.1 用户不能放入不正确的信息
    1.3.2 数量和价格必须是数字我的异常处理还没有做呢?
    另外我创建了文件, 不想覆盖原有的数据,应该怎么做啊!
      

  2.   

    do-while循环少了一个do,应该是这样
    String RecordNumber;
            String name;
            String quantity;
            String cost;
            
    // create new buffered output writer
            PrintWriter pw = new PrintWriter(new
                    BufferedWriter(new FileWriter("hardware.dat")));
            
            Scanner console = new Scanner(System.in);
            
            do{
            System.out.print("Enter the RecordNumber");
            RecordNumber = console.nextLine();      
            
            System.out.println("Enter Tool Name");        
            name = console.nextLine();
            
            System.out.println("Enter Quantity");        
            quantity = console.nextLine();
            
            System.out.println("Cost per item(A$)");        
            cost = console.nextLine();
            
            pw.println(""+name+"\t"+quantity+"\t"+cost);
            System.out.println("Continue Y/N ?");
           
            
            } while  (console.nextLine().charAt(0) == 'Y');
            pw.close();