JustAnswer > Programming
Ask A Question|Register|Login|Help
JustAnswer

Programming

Ask a Programming Question, Get an Answer ASAP!

Have your own Programming question?

5 Programmers are Online Now
characters left:
Not a Programming Question?

Related Programming Topics:

  • Run
  • ,
  • Code
  • ,
  • File
  • ,
  • Java
  • ,
  • Text
  • ,
  • User
  • ,
  • Print
  • ,
  • Works
  • ,
  • Write
  • ,
  • Format
Bookmark and Share

Question

Rafael Martins,


This is supposed to read product id, product name and product price entered from the user and writes this information to a file. The application should repeatedly prompt for this product information until terminated by the user.

Here is my code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class InventoryInput
{

public static void main(String[] args)
{
int productid = 0;
String productname;
float productprice;
NumberFormat priceformat = new DecimalFormat("0.99");
try {
if (args.length != 1) {
System.out.println("Usage: java InventoryInput filename");
System.out.println("\tfilename is the full path where the inventory file is to be written.");
System.exit(0);
}
BufferedWriter writer = new BufferedWriter(new FileWriter(args[0]));
String instr = null;
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
do {
productid = 0;
System.out.println("Please enter a product number or -1 to quit:");
instr = input.readLine().trim();
try {
productid = Integer.parseInt(instr);
if (productid > 0) {
System.out.println("Please enter a product name:");
productname = input.readLine().trim();
System.out.println("Please enter a product price:");
instr = input.readLine().trim();
try {
productprice = Float.parseFloat(instr);
writer.write(String.valueOf(productid));
writer.write(' ');
writer.write(productname);
writer.write(' ');
writer.write(priceformat.format(productprice));
writer.write('\n');
} catch (NumberFormatException nfe) {
System.out.println("Product price must be an numeric value, ex: 19.50");
}
}
} catch (NumberFormatException nfe) {
System.out.println("Product number must be an integer value, ex: 1001");
}
} while (productid != -1);
writer.close();
input.close();
System.exit(0);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

Submitted: 182 days and 20 hours ago.
Category: Programming
Value: $9
Status: CLOSED
+
Read More

Optional Information

Browser: Firefox

Posted by Rafael Martins 182 days and 19 hours ago.

Answer

Hello,

Your code is perfect as you can see in the screenshot bellow:

graphic
View Full Image


Let me know you need further assistance.

Thanks.

Edited by Rafael Martins on 5/24/2009 at 2:39 AM

182 days and 5 hours ago.

Reply

It isn't working correctly on my end

Posted by Rafael Martins 182 days and 4 hours ago.

Answer

Hi,

Check out this document: How to configure the run arguments in NetBeans.doc

Here is the NetBeans project: InventoryInput

Hope this helps.

Regards,
XXXXXX XXXXXXX.

181 days and 3 hours ago.

Reply

How can I make it display all the data that has been input? e.t. product id's, names, and prices of each

Posted by Rafael Martins 181 days and 2 hours ago.

Info Request

Hi,

I am sorry but I do not understand what you want.
The program already works as requested...

Could you be more specific?

Thanks.

181 days ago.

Reply

I need it to print what was entered in like this when -1 is entered:

10235 Hammer 9.95
22354 nail .09

Posted by Rafael Martins 180 days and 23 hours ago.

Info Request

Ok. Please wait a while.

Accepted Answer

Ok,

Here you go:

Code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
* This is supposed to read product id, product name and product price entered
* from the user and writes this information to a file. The application should
* repeatedly prompt for this product information until terminated by the user.
*/

public class InventoryInput {

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: java InventoryInput filename");
            System.out
                    .println("\tfilename is the full path where the inventory file is to be written.");
            System.exit(1);
        }

        int productid = 0;
        String productname;
        float productprice;
        NumberFormat priceformat = new DecimalFormat("0.99");
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(args[0]));
            String instr = null;
            final BufferedReader input = new BufferedReader(
                    new InputStreamReader(System.in));
            do {
               productid = 0;
               System.out
                        .println("Please enter a product number or -1 to quit:");
               instr = input.readLine().trim();
               try {
                    productid = Integer.parseInt(instr);
                    if (productid > 0) {
                        System.out.println("Please enter a product name:");
                        productname = input.readLine().trim();
                        System.out.println("Please enter a product price:");
                        instr = input.readLine().trim();
                        try {
                            productprice = Float.parseFloat(instr);
                            writer.write(String.valueOf(productid));
                            writer.write(' ');
                            writer.write(productname);
                            writer.write(' ');
                            writer.write(priceformat.format(productprice));
                            writer.write('\n');
                        } catch (NumberFormatException nfe) {
                            System.out
                                    .println("Product price must be an numeric value, ex: 19.50");
                        }
                    }
               } catch (NumberFormatException nfe) {
                    System.out
                            .println("Product number must be an integer value, ex: 1001");
               }
            } while (productid != -1);
            writer.close();
            input.close();

            // reads in the input file and prints out its contents.
            BufferedReader reader = new BufferedReader(new FileReader(args[0]));
            while (reader.ready()) {
               System.out.println(reader.readLine());
            }
            reader.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

    }
}


Hope this helps.

Thank you.

Picture
Expert: Rafael Martins
Pos. Feedback: 100.0 %
Accepts: 
Answered: 5/25/2009

Programming Tutor

Desktop, Mobile and Web Developer. 6 years of experience. Creative solutions provider.

+
Read More

Related Programming Questions

  • I have developed a spreadsheet that has 1 page with all the
  • Hi Jerry, you helped me (Japie1973) successfully to create
  • how to get data from one cell into multiple columns so that
  • I have this question that I was trying to figure out and don...
  • we have cadvance system and we had to reload the product sym...
  • I am willing to pay $40 for this, I was going to do $10 and
  • how can I maintain focus within a textbox while validating
  • Using the Internet and other resources, explain modularizati...



Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.
Question List | Become an Expert | Terms of Service | Security & Privacy | About Us
© 2003-2009 JustAnswer Corp.