Results 1 to 5 of 5
  1. #1
    bwissman is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Inventory Program Part 3 assistance needed

    I have written a code for this assignment and have most of it running error free. My problems is in my main class and according to my teacher I have incorrectly declared my main method. I followed an example that he gave us so I am not sure how I declared it incorrectly. I will post my main class here and any help is greatly appreciated.


    package inventory.program.pkg3;

    import java.io.*;
    import java.util.*;
    import java.text.*;


    public class InventoryProgram3
    {

    public InventoryProgram3() throws IOException
    {
    Inventory Inventory = new Inventory();

    int counter = 0;

    BufferedReader input = new BufferedReader (new InputStreamReader (System.in));

    System.out.println("Welcome to the the Inventory Program!");

    while (counter < 10)
    {
    System.out.print("Please enter the name of the book or enter the word exit to leave the program: ");
    String bookName = input.readLine();

    if (bookName.equalsIgnoreCase("exit"))
    {
    break;
    }

    System.out.print("Please enter the item number for this book: ");
    String itemNumber = input.readLine();

    System.out.print("Please enter the author of this book: ");
    String bookAuthor = input.readLine();

    System.out.print("Please enter the number for how many of these books are available: ");
    int unitsAvailable = Integer.valueOf(input.readLine()).intValue();

    while (unitsAvailable < 0)
    {
    System.out.println("You have entered an invalid number pleasae use a positive number for the available units: ");
    unitsAvailable = Integer.valueOf(input.readLine()).intValue();
    }

    System.out.print("Please enter the price for each book: ");
    double individualPrice = Double.valueOf(input.readLine()).doubleValue();

    while (individualPrice <= 0.0)
    {
    System.out.println("You have entered an invalid number please enter a positive number for the price of each book: ");
    individualPrice = Double.valueOf(input.readLine()).doubleValue();
    }

    Book thebook = new Book(bookName, itemNumber, bookAuthor, unitsAvailable, individualPrice);

    counter++;

    System.out.println();
    }

    NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();

    Inventory.sortInventory();

    for (int i = 0; i < Inventory.getnumberofBooks(); i++)
    {
    Book thebook = Inventory.getBook(i);

    System.out.println("The title of the book is: " + thebook.getbookName());
    System.out.println("The item number for this book is: " + thebook.getitemNumber());
    System.out.println("The author of this book is: " + thebook.getbookAuthor());
    System.out.println("You have " + thebook.getunitsAvailable() + " currently in stock");
    System.out.println("The price for each book is: " + moneyFormat.format(thebook.getindividualPrice()));
    System.out.println("The restocking fee for this item is: " + moneyFormat.format(thebook.restockingFee));
    System.out.println("The value of this item is: " + moneyFormat.format(thebook.getinventoryTotal));

    System.out.println();

    System.out.println("The Total amount your inventory is worth is: " + moneyFormat.format(thebook.gettotalinventoryValue) );

    }

    public static void main(String args[]) throws IOException // this line has the error stating "illegal start of expression""//
    {
    new InventoryProgram3();// This line has a caution stating "new instance has been ignored"//
    }

    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Inventory Program Part 3 assistance needed

    You have two things which are not "correct"

    1. your main declares a thrown exception. That makes no sense since the main is the top of the program, there is nothing above it that it can throw an exception to
    2. your entire application is in the constructor of the class, which in any normal application should not and would not ever happen, you'd put the actual application logic in different methods of the class (and possibly other classes).

    That's what that 'new instance has been ignored' warning is all about, you are creating an object and immediately throwing it away again. That is under normal circumstances an indication that you're doing something wrong. But in your case it happens to work since you stuffed everything in the constructor.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    bwissman is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: Inventory Program Part 3 assistance needed

    Alright, the example I was following has it set up that way as well. At this point I am not sure what I should do all of this is new to me. Should I rewrite the entire main class? Also should the exception at the top be removed because if I do I then get more errors throughout the rest of the main class.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Inventory Program Part 3 assistance needed

    Yes you need to remove that throws IOException, and then you need to fix the code so it does what it is supposed to do. And that means you need to study exception handling.

    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    That may be a bit too much for you at this point so I'll just show you how to work around your problem so you can delay learning about exceptions until the proper moment. Do this:

    Java Code:
    public static void main(String[] args){
    
      try{
         new InventoryProgram3();
      } catch(Throwable t){
         t.printStackTrace();
      }
    }
    But I stress: make learning about exception handling a priority. And I may have foiled your instructor's evil plot to try and get you to research this stuff on your own initiative; in that case apologize to him for me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: Inventory Program Part 3 assistance needed

    To remove it without errors you need a Try Catch statement around the Buffer Reader because that is what throws the exception and by using try catch it will catch the exception right there and will not need to have throws IOException anywhere else because the exception won't make it that far
    With the right know how, anything is possible

Similar Threads

  1. Inventory Part 4 assistance
    By ladykrimson in forum New To Java
    Replies: 26
    Last Post: 11-05-2010, 09:56 PM
  2. Help with inventory program part 5 PLEASE!
    By Exether in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 06:25 AM
  3. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 01:30 AM
  4. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 05:57 AM
  5. Inventory Program Part 3 - DUE TODAY (7/28/08)
    By ljk8950 in forum New To Java
    Replies: 7
    Last Post: 07-27-2008, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •