Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.*;
import java.io.*;

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

tokens=0;
while((line = bufferedReader.readLine()) != null) {

System.out.println(line);
while (tokens < 4){
x=line.charAt(i);
if (x==','){
parts[tokens]=line.substring(lt,i);
parts[tokens].trim();
System.out.println(parts[tokens]);
tokens++;
System.out.println(i);
lt=i+2;
}
i++;
}

System.out.println(i);
if (tokens==4){

int a=line.length();
System.out.println(i+"."+a);
/*parts[5]=line.substring(i,a-1);
parts[5].trim();
System.out.println(parts[5]);*/
i=0;
tokens=0;
lt=0;

}

System.out.println("...");

//parts[tokens]=line.substring(lt,i);
//System.out.println(parts[tokens]);
}

// Always close files.
bufferedReader.close();{
}
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:

import java.io.*;

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

public Product(double price, String name){
this.price=price;
this.name=name;
this.id=id_all;
id_all++;
}

public Product(double price, String name, int id){
this.price=price;
this.name=name;
this.id=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 (this.name.equals(otherproduct.name)) {
if (this.price == otherproduct.price) {
if (this.id == otherproduct.id) {
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)
Posted
Updated 18-May-18 1:45am

1 solution

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900