Results 1 to 3 of 3
  1. #1
    wholegrain is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Question Can you rewrite this method without using LinkedList?

    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;
    }

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default Re: Can you rewrite this method without using LinkedList?

    Your formatted code is below so it is readable. Yes you could use a different data structure in this method. Whether it is a good idea would depend on how the list is being used. For instance, if it the consuming method requires random access an array may be better.

    Java Code:
    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 " +
                      ameFile);
                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));
                       list.add(new People(name, sex, height, weight, number));
                  }
             }
             enter.close();
          }
          return list;
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,045
    Rep Power
    23

    Default Re: Can you rewrite this method without using LinkedList?

    When you post a question here, we expect you to reply to responses. Not just post one more and yet another while ignoring Fubarable's response on
    http://www.java-forums.org/new-java/...nto-array.html

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. help me rewrite my long code.
    By Nes_java in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 12:52 AM
  2. How to use linkedlist 'add' method without iterator
    By plexus0208 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 09:27 PM
  3. How to rewrite this as applet?
    By tpname in forum Java Applets
    Replies: 3
    Last Post: 01-01-2010, 07:19 PM
  4. [SOLVED] url rewrite
    By mtyoung in forum Java Servlet
    Replies: 3
    Last Post: 02-02-2009, 11:12 AM
  5. rewrite mousedown code
    By brahms666 in forum New To Java
    Replies: 0
    Last Post: 11-26-2008, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •