Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Java Help


ghost's Avatar
0 0

Hi there! I must do a little lab for a course, normaly really easy to do, but I'm stuck with this little bug in my code and, misteriously, can't find the error.

Here's the code

public void initProduits() { Vector<Produits> listeProduits = new Vector<Produits>(); Produits produit = new Produits();

	try
	{
		BufferedReader fichier = new BufferedReader(new FileReader(&quot;PRODUITS.TXT&quot;));
		while(fichier.readLine() != null)
		{
			String ligne = fichier.readLine();
			
			produit.setNoProduits(Integer.parseInt(ligne.substring(0,3).trim()));
			produit.setDescripProduits(ligne.substring(4,29).trim());
			produit.setPrixProduits(Double.parseDouble(ligne.substring(30,36).trim()));
			produit.setQtDispo(Integer.parseInt(ligne.substring(37).trim()));
			
			listeProduits.addElement(produit);
		}
		fichier.close();
	}catch(IOException e)
	{e.printStackTrace();}

}

-It refers to another class Produits (for the getters and setters) -All in all, this is to make an Inventory in a Vector named "listeProduits" -I think it's from the BufferedReader but I can't find the problem. -Yes, I have the txt document in the same folder (Using Eclipse)

If anybody can find the bug, it should really help me, thx.

PS: ENG isn't my first language, plz don't get mad at me :happy:


ghost's Avatar
0 0

well, i dont know if you have a run time error or a compile error, you didnt say…. but you will have problems since your while loop while(ficher.readline()!=null) that IS reading the line, so the read line in the loop gets the next you only get every other line with that method.


ghost's Avatar
0 0

I speak french fluently, Samurai, let me help you out in translating/conveying your thought, lol. clears throat

@FrostPulse:

Samurai veux seulement dire que ta methode de lire les lignes dans ton fichier est peut etre non-standard, ou meme fausse.

C'est a dire que la section ou tu as le code suivant a besoin de correction: > while(ficher.readline()!=null)

En plus de cela, tu n'as pas preciser les erreurs de compilation. Cela aidera la communautee a mieux t'aider. A+ mec.


ghost's Avatar
0 0

sry it is a runtime error and it is not the while cause it would only make the information wrong. Right now, it just dont want to run and sincerely, I do not completly understand this in the console( the prob is, our teachers don't even know what exactly it means….so we are merely on ourselfs) :

java.io.FileNotFoundException: PRODUITS.txt (Le fichier spécifié est introuvable) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at java.io.FileReader.<init>(Unknown Source) at lab2.Application.initProduits(Application.java:20) at lab2.Application.main(Application.java:10) Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 >= 0 at java.util.Vector.elementAt(Unknown Source) at lab2.Application.initProduits(Application.java:36) at lab2.Application.main(Application.java:10)


ghost's Avatar
0 0

@Netfish

Je comprends ce qu'il veut dire, mais le hic c'est que même si mon while ne serait pas standard, sa ne m'empêcherait pas de pouvoir faire fonctionner le code, les erreurs seraient à la sortie.

Enfin, j'ai modifié un peu le code lisant une ligne avant d'entré dans le while mais sa ne change pas grand chose. Sa m'énerve et je veux absolument comprendre cette erreur, je suis certain que c'est une erreur de débutant en plus….

New code: public static void initProduits() { Vector<Produits> listeProduits = new Vector<Produits>(); Produits produit = new Produits();

	try
	{
		BufferedReader fichier = new BufferedReader(new FileReader(&quot;PRODUITS.txt&quot;));
		String ligne = fichier.readLine();
		while(ligne == null)
		{
			ligne = fichier.readLine();
			
			produit.setNoProduits(Integer.parseInt(ligne.substring(0,3).trim()));
			produit.setDescripProduits(ligne.substring(4,29).trim());
			produit.setPrixProduits(Double.parseDouble(ligne.substring(30,36).trim()));
			produit.setQtDispo(Integer.parseInt(ligne.substring(37).trim()));
			
			listeProduits.addElement(produit);
		}
		fichier.close();
	}catch(IOException e)
	{e.printStackTrace();}

}


ghost's Avatar
0 0

you're trying to write to PRODUITS.TXT, but first do some checking:

  • Did you already create the file.
  • Do you have rights to read/write to it.
  • Are you calling the file from the right directory.

Where are your imported library files specified, you might be missing one? That's probably what the other errors are from. Show what you've imported.

Oh, yah, also show your "PRODUIT" class if possible, that'll help us troubleshoot the code more efficiently (and faster).


ghost's Avatar
0 0

netfish wrote: you're trying to write to PRODUITS.TXT, but first do some checking:

  • Did you already create the file.
  • Do you have rights to read/write to it.
  • Are you calling the file from the right directory.

Where are your imported library files specified, you might be missing one? That's probably what the other errors are from. Show what you've imported.

Oh, yah, also show your "PRODUIT" class if possible, that'll help us troubleshoot the code more efficiently (and faster).

I'm trying to read from it ;)

  • Already created
  • I have the rights
  • I've put the file in my project folder(with the .java and .class files)

Imports:

import java.util.; import java.io.;

Class Produits:

public class Produits { //Variables d'instance private int noProduits, qtDispo; private double prixProduits; private String descripProduits;

//Getters and Setters des instances de la classe
public String getDescripProduits() {return descripProduits;}
public void setDescripProduits(String descripProduit) {this.descripProduits = descripProduit;}

public int getNoProduits() {return noProduits;}
public void setNoProduits(int noProduit) {this.noProduits = noProduit;}

public double getPrixProduits() {return prixProduits;}
public void setPrixProduits(double prixProduit) {this.prixProduits = prixProduit;}

public int getQtDispo() {return qtDispo;}
public void setQtDispo(int qtDispo) {this.qtDispo = qtDispo;}

}

I'm using the default constructor from the class Produits


ghost's Avatar
0 0

50% of you errors are coming from FileInput / InpuStream exceptions. It has something to do with the file. It might NOT be obvious or spot what's wrong, but re-check till you get it.

As is JAVA. It's almost errors never lie! That's the power of the language.

I checked your syntax, and class/objects/methods interact well, and I don't see any apparent mistakes. I agree with the compiler (for the time being), lol. Sorry, that's all I can say for now. Good luck!


ghost's Avatar
0 0

Found the problem…:D

The file wasn't found, lab2 is a package and I needed to put the file in the project folder, not the package one.

Thx a lot for your help.


ghost's Avatar
0 0

:o) like I said earlier:

As is JAVA. It's almost errors never lie! That's the power of the language.

CONGRATS!