Member Avatar for begueradj

Hello people,

I designed this class:

package aiproject;
/*
 * This class has the task to read the data stored in a file called "fichier.txt"
 * line by line.
 * The read lines will be put inside a List called lignesDeMonfichier
 * Also, this class counts the number of lines that my file contains
 * */

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ReadMyFile{
	
	protected List<String>lignesDeMonFichier=new ArrayList<String>();
	protected int nombreDeLignesDeMonFichier;
	private int n=0;
	
	/*
	 * reads my file line by line
	 * */
	public void lireFichier(){
		try{
			/*
			 * the file to be read must be named "fichier.txt"
			 * */
			FileInputStream fistream=new FileInputStream("fichier.txt");
			DataInputStream in=new DataInputStream(fistream);
			BufferedReader br=new BufferedReader(new InputStreamReader(in));
			String strLine;
			while((strLine=br.readLine())!=null){
				lignesDeMonFichier.add(strLine);
				n++;
			}
			n=nombreDeLignesDeMonFichier;
		}catch(Exception e){
			System.out.println("Error: "+e.getMessage());
		}
	}
	
	/*
	 * return the number of lines being read in my file
	 * */
	public int getNumberOfLinesInMyFile(){
		return nombreDeLignesDeMonFichier;
	}
	/*
	 * return the list of string lines being read in my file
	 * */
	public List<String> getLinesOfMyFile(){
		return lignesDeMonFichier;
	}
}

Then I designed this class from which I called the method lireFichier() of the former class:

package aiproject;
/*
 * This class stores after conversion the data stored in the file that has been
 * read by ReadMyFile class into a 2 dimensional array of integers
 * */
public class StoreFileDataIntoIntegerMatrix{
	ReadMyFile readF=new ReadMyFile();
	readF.lireFichier();		
	
}

But it shows me this error with eclipse that concerns the line: readF.lireFichier():
Syntax error on token "lireFichier", Identifier expected after this token

Can you explain me what is this error and how to fix it please ?

Regards

Methods can only be called from within other methods, not in the class declaration like you did. Every method is called from within another method, starting with the main(String[] args) method.

Also, I noticed you putting n=nombreDeLignesDeMonFichier, while it should actually be the other way around. You haven't initialised nombreDeLignesDeMonFichier, so now n will be zero instead of your number of lines to be n.

x=y is not the same as y=x in Java.

Good luck

Member Avatar for begueradj

Methods can only be called from within other methods, not in the class declaration like you did. Every method is called from within another method, starting with the main(String[] args) method.

Also, I noticed you putting n=nombreDeLignesDeMonFichier, while it should actually be the other way around. You haven't initialised nombreDeLignesDeMonFichier, so now n will be zero instead of your number of lines to be n.

x=y is not the same as y=x in Java.

Good luck

Thank you very much for your help

Yes, you are right about n=nombreDeLignesDeMonFichier

But if calling a method is only right when calling it from an other method then why when I do as follow it does not show me any error ?:

package aiproject;
      /*   
       * This class stores after conversion the data stored in the file that has been
         * read by ReadMyFile class into a 2 dimensional array of integers
          * */
   
      public class StoreFileDataIntoIntegerMatrix{
      ReadMyFile readF=new ReadMyFile();
      int b=   readF.lireFichier();
         
   }

Regards

Member Avatar for begueradj

Methods can only be called from within other methods, not in the class declaration like you did. Every method is called from within another method, starting with the main(String[] args) method.

Also, I noticed you putting n=nombreDeLignesDeMonFichier, while it should actually be the other way around. You haven't initialised nombreDeLignesDeMonFichier, so now n will be zero instead of your number of lines to be n.

x=y is not the same as y=x in Java.

Good luck

Sorry, I meant calling like this at the same point as above within the same class:

int b getNumberOfLinesInMyFile();

It does not show any error

I believe now that is because you're doing an initialisation, and I don't know what you mean with your last post. Where did you put that code, or is it just the definition of your method?

Member Avatar for begueradj

I believe now that is because you're doing an initialisation, and I don't know what you mean with your last post. Where did you put that code, or is it just the definition of your method?

Thank you again.
I meant when I called the method not inside a method area as follows it did not show a problem: why ?

package aiproject;
      /*   
       * This class stores after conversion the data stored in the file that has been
         * read by ReadMyFile class into a 2 dimensional array of integers
          * */
   
      public class StoreFileDataIntoIntegerMatrix{
      ReadMyFile readF=new ReadMyFile();
      int b=   readF.getNumberOfLinesInMyFile();
         
   }

When you define a variable you can initialise it with a single expression that returns a suitable value. You already know about int a = 99; or maybe int secondsPerYear = 365*24*60*60; so this is just a slightly more complex example int b = readF.getNumberOfLinesInMyFile(); What you cannot have is an initialisation expression outside a variable declaration.

(To be exact - there are also things called instance initialisers, but you don't need to worry about those now.)

ps: You don't need your lignesDeMonFichier variable because ArrayLists have a size() method that gives you the number you need. By calculating it yourself you just do more work, and create the possibility of getting it wrong sometime.

Member Avatar for begueradj

When you define a variable you can initialise it with a single expression that returns a suitable value. You already know about int a = 99; or maybe int secondsPerYear = 365*24*60*60; so this is just a slightly more complex example int b = readF.getNumberOfLinesInMyFile(); What you cannot have is an initialisation expression outside a variable declaration.


(To be exact - there are also things called instance initialisers, but you don't need to worry about those now.)

Thank you very much for your help. You explained me very well. Thank you a lot.
Yours

de rien.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.