Click here to Skip to main content
13,836,537 members
Rate this:
Please Sign up or sign in to vote.
See more:
Ok guys, i will explain what is the problem here. I'm trying to read a file that contains some informations that i will use later on to create a "product". How can i do it? Because i have trying for few weeks to solve it :( and the deadline is close. Pls help :(

What I have tried:

This is the MAIN class

import java.util.*;

public class VendingMachine{
public static void main(String[] args)throws IOException{

Product product;
Display display;
ProductStorage product_storage;
CoinManager coin_manager;

String parts[]=new String[5];
int tokens;
int i=0;
int lt=0;
char x;
String aaa;

// The name of the file to open.
String fileName = "products.txt";

// This will reference one line at a time
String line = null;

try {
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);

//*StringTokenizer str = new StringTokenizer ();*

while((line = bufferedReader.readLine()) != null) {

while (tokens < 4){
if (x==','){

if (tokens==4){

int a=line.length();




// Always close files.
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");

this is the "product" file:


public class Product{
double price;
String name;
int id;
static int id_all = 0;

public Product(double price, String name){

public Product(double price, String name, int id){

public double getprice(){
return price;

public void setprice (double price){
this.price = price;

public String getname(){
return name;

public boolean equals(Product otherproduct){
if ( {
if (this.price == otherproduct.price) {
if ( == {
return true;
return false;

This is the text file that i have to read:

1, soft drink, cocacola, 330, 1.00
2, water, water, 500, 0.50
3, water, water, 1000, 0.50
4, juice, keanita, 250, 1.00
5, chocolate, lacta, 100, 2.00

(just number, the type of drink, the name, the stock, and the price)
Updated 18-May-18 2:45am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You read the file but then do not do anything with the content. Start with a Collection object(Trail: Collections (The Java™ Tutorials)[^]) that will hold a list of products. Then as you read each line, split it into the relevant tokens and create a new Product object that will contain the data. Add this object to your collection and repeat to the end of the file. What you do then is up to you, but you should have a nice collection of products that you can manipulate.
Member 13833497 18-May-18 7:56am
Ok, let me see if i understand. I have to: first, i have to create an new Product object as i am reading the text file? but how i can do that for more than one project? for example. i can right it 4 times. but if someone will add a new product into the text file, the program will dont read it. Im i right or im more confused right now? xD
Richard MacCutchan 18-May-18 8:04am
It does not matter how many lines of detail are in the file. Your program should do what I suggested above and just read every line. The main issue is what you do with the data once you have the collection of products. Are they supposed to be saved somewhere (e.g. in a database), use to create some sort of report, etc.?
Member 13833497 18-May-18 8:08am
i have to do a "vendingmachine".. so.. with the information of the text file i have to make a gui so someone can to see what products is selling, the price and if the quantity is 0.. visibility = false;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190114.1 | Last Updated 18 May 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100