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)