-1

This is my homework assignment... "You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand, and the cost of each one. Write a program that creates a random-access file "hardware.dat". Your program should allow you to input the data for each tool and write your data to the file it created. Use the following information to start your file: ..." Then it gives a list of the info.

These are what I have for my assignment.. (classes below) the second (Hardware class) I have no issues with it, but I just want to make sure that it's right. But I'm having issues with RandomAccessFile in the first link. I have no clue if I'm doing it right. It keeps saying "RandomAccessFile is already defined in this compilation" whenever I hover over "import java.io.RandomAccessFile;" what does that mean?? (I can't paste the links without code being included with it) What am I doing wrong?

RandomAccessFile class:

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scanner;

public class RandomAccessFile  {
    public static void main(String[] args) 
    {
        RandomAccessFile file;
        Scanner sc = new Scanner(System.in);

        int toolId;
        String toolName;
        int quantity;
        double cost;

        System.out.println("Please Input Tool ID: ");
        toolId = sc.nextInt();

        System.out.println("Please Input Tool Name: ");
        sc.nextLine();
        toolName = sc.nextLine();

        System.out.println("Please Input the Quantity: ");
        quantity = sc.nextInt();

        System.out.println("Please Input the Price: ");
        cost = sc.nextDouble();

            try
            {
                file = new RandomAccessFile(new File("hardware.dat"), "rw");
                long FileSize = file.length();
                file.seek(FileSize);

                file.writeInt(toolId);

                file.writeUTF(toolName);
                for(int i = 0; i < 24- toolName.length(); i++)
                {
                    file.writeByte(24);
                }

                file.writeInt(quantity);

                file.writeDouble(cost);

                System.out.println("Item added to inventory");
                file.close();
            }

            catch (FileNotFoundException e)
            {
                System.err.println("File not found. Check Project Folder and if you have read and write permissions to it");
            }

            catch (IOException e)
            {
                e.getStackTrace();
            }
    }
     }

Hardware Class:

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;

public class Hardware 
{

    private static final int RECORD = 24;

    public static void main(String[] args) 
    {
        RandomAccessFile file;
        int toolId = 0;
        String toolName = null;
        int quantity = 0;
        double cost = 0.00;

        try 
        {
            file = new RandomAccessFile(new File("hardware.dat"), "rw");

            long FileSize = file.length();
            file.seek(0);
            long NUMRecords = FileSize / RECORD;
            for (int j = 0; j < NUMRecords; j++) 
            {

                toolId = file.readInt();

                toolName = file.readUTF();
                for (int i = 0; i < 24- toolName.length(); i++) 
                {
                    file.readByte();
                }

                quantity = file.readInt();

                cost = file.readDouble();

                System.out.println("Tool ID: "  + toolId + "  Tool Name: " + toolName + "  Quantity: " + quantity + "  Cost:  $" + cost);

            }

            file.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("File not found. Check Project Folder and if you have read and write permissions to it");
        } 

        catch (IOException e) 
        {
            e.getStackTrace();
        }

    }
}
Norcino
  • 5,850
  • 6
  • 25
  • 42
hts95
  • 103
  • 1
  • 1
  • 9

2 Answers2

0

I can't access your links but apparently you have named your class as RandomAccessFile ? If so try to change its name, because at the same time you import another class named java.io.RandomAccessFile, hence the error.

nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

I figured it out. It was something dumb/silly. My class name was "RandomAccessFile" (to help me keep things organized for class) and once I changed that then the code ran fine...

hts95
  • 103
  • 1
  • 1
  • 9