I am attempting to read data from a comma delimited file.
The delimited file reads as follows.

A823,Laser-Guided Scissors,Scissors allow cuts without a pencil and ruler.,17.99
7437,TIX LED Clock,Stylish desk or wall clock uses LED patterns to tell time.,49.99
8DB4,Multi-color LED Flashlight,This bright flashlight illuminates in ten differnet colors.,24.99
A565,Nerf Elimination Office Warfare Set,Set of four blasters for instant office warfare.,29.99
61B7,The Red Swingline Stapler,I believe we have your stapler,21.99

I am to then store the data in one or more arrays, and accept input from the user consisting of the product number. Once the user inputs the product number I will display the product associated with that product number and give the user the following options.

  • purchase the item
  • look at another item
  • finish their purchase
  • exit the application

When the user has finished with their browsing and making selections for purchase I will display an invoice of the user’s purchases including tax and shipping cost.

Now I have only just begun the code on this project and I am already running into a problem and that is getting the data from the infile.

The small bit of code I have written thus far is:

String product;
        int x = 0;
        //Get User Input for Product Number
       System.out.print("Enter the product number of the item you wish to view [ex A823]: ");
        product = scan.nextLine();
        //Scan the infile
        Scanner scan2 = new Scanner ( new File("F:/Java/Final/src/product.txt")); 
        //Store the current read line from the infile
        String line = scan2.nextLine();
        //Display the read line that is currently being focused
        String abbrev = scan2.next();
       if (abbrev.equals(product))
           String word = scan2.next();
            x -= 1;
        abbrev = scan2.next();
        }}while(x >= 0);

I do realize there isn't much here yet but I wanted to get the handling of reading the infile under control before moving to the next parts of what is required. Any help or suggestions would be appreciated.

Well, using x -= 1; to represent the much simpler and easier to read boolean found=true; isn't doing you any favors for one thing. You also need to read the API doc on the Scanner class, especially the general portion at the top and consider that in the context of what you see the program doing.

This article has been dead for over six months. Start a new discussion instead.