2 Replies - 212 Views - Last Post: 03 April 2019 - 09:04 AM Rate Topic: -----

#1 hts   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-April 19

Java RandomAccessFile

Posted 03 April 2019 - 08:49 AM

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.. the second one I have no issues with it, but I just want to make sure that it's right. But I'm having issues with import java.io.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?? If I get rid of it, then it messes up other RandomAccessFile parts of my code... This is probably really simple but I'm still new to Java..

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();
            }
    }
    
}


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();
        }
        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Java RandomAccessFile

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 771
  • View blog
  • Posts: 5,774
  • Joined: 25-December 13

Re: Java RandomAccessFile

Posted 03 April 2019 - 08:51 AM

Quote

It keeps saying "RandomAccessFile is already defined in this compilation"

Please copy the full text of the error message and paste it here. It has important info about the error.

Your class has that same name so the compiler is confused. Rename your class so it is not the same name as the Java SE's class.

This post has been edited by NormR: 03 April 2019 - 08:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 hts   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-April 19

Re: Java RandomAccessFile

Posted 03 April 2019 - 09:04 AM

View PostNormR, on 03 April 2019 - 08:51 AM, said:

Quote

It keeps saying "RandomAccessFile is already defined in this compilation"

Please copy the full text of the error message and paste it here. It has important info about the error.

Your class has that same name so the compiler is confused. Rename your class so it is not the same name as the Java SE's class.


Oh my goodness... okay I see that now.. I didn't even realize that the class name was the issue... changed the class name and now it runs fine... thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1