Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you rewrite this method without using LinkedList?
#1
static LinkedList<People> create(String nameFile)
throws IOException
{ LinkedList<People> list = new LinkedList<People>();
boolean Fileexists = true ;

FileReader fi = null;


try {
fi = new FileReader (nameFile) ;
}

catch ( java.io.FileNotFoundException erreur) {
System.out.println("Probleme d'ouvrir le fichier " +
nameFile);
Fileexists = false ;
}

if (Fileexists) {


BufferedReader enter = new BufferedReader(fi);
boolean endFile = false ;


while ( !endFile ) {


String aLine = enter.readLine();


if (aLine == null)
endFile = true ;
else {

String name = aLine.substring(0, 10);
char sex = aLine.charAt(30);
double height = Double.parseDouble(aLine.substring(37, 41));
double weight = Double.parseDouble(aLine.substring(51, 56).trim());
int number = Integer.parseInt(aLine.substring(64));

liste.add(new People(name, sex, height, weight, number));
}
}
enter.close();
}

return list;
}
Reply
#2
Could you please write your code in code tag [ code ] you code goes here [/ code ] remove spaces thanks ;)
Reply
#3
Why do you want to use anything else other than LinkedList? LinkedList makes sorting easier. Other than LinkedList you can use object arrays. But in arrays its difficult to add objects between two existing objects. Anyway, here's what you want:

Assuming that People() is pointing to a class constructor, the following code can be used.

People Class:

public class People {

public People(String name, char sex, double height, double weight, int number) {

//Your method code here

}
}


----------------------------------------------------------------------

ObjectList Class:

import java.io.*;

public class ObjectList {

static Object objList[];
private static Object[] create(String nameFile) {

try {

FileReader fi = new FileReader(nameFile);
BufferedReader enter = new BufferedReader(fi);

int varCounter = 0;

String aLine = enter.readLine();

while(aLine instanceof String) {

String name = aLine.substring(0, 10);
char sex = aLine.charAt(30);
double height = Double.parseDouble(aLine.substring(37, 41));
double weight = Double.parseDouble(aLine.substring(51, 56).trim());
int number = Integer.parseInt(aLine.substring(64));

objList[varCounter] = (new People(name, sex, height, weight, number));
varCounter++;

aLine = enter.readLine();
}

enter.close();
}

catch(Exception error) {
System.out.println("Error: "+error.toString());
}

return objList;
}
}


Did not use code tags as the text inside it is unreadable.
Reply
#4
BufferedReader enter = new BufferedReader(fi);
boolean endFile = false ;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)