The Artima Developer Community
Sponsored Link

Java Answers Forum
reading file into a Singly LinkedList

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jackie

Posts: 4
Nickname: jackie
Registered: Apr, 2002

reading file into a Singly LinkedList Posted: Apr 17, 2002 5:52 AM
Reply to this message Reply
Advertisement
Hi there,

I can read the file correctly but it seems that the program is creating a new object everytime it's reading the data into the linkedList instead of adding it into the linkedlist. I'm not quite sure what is happening with my code. Can anyone please tell me what is missing/ what i did wrong. I would really appreciate any help.

//===================================================
//Sample code for reading file

//import corejava.*;
import java.io.*;
import java.util.*;
import java.lang.*;

class FileInput

/* This class is used to load the file in from
the user */

{
public Keyboard kb = new Keyboard();


public String getFileName(int message)
{
/* Method: getFileName
Description: This function gets the file name
Pre-condition:
Post-condition: Returns the file name entered
by the user. */


Output output = new Output();

output.clearScreen();
if (message == 2)
System.out.println("\t\t\t\t" + "Load
Attendee File");

// put gap under heading
for(int i=0; i<20; i++)
System.out.println();

if (message == 2)
System.out.println();
String fileName = kb.getLine(" Please
enter the attendees file name in the
following" + "\n" + "drive
letter:\\filename.extension : ");

System.out.print(fileName); //USE TO CHECK IF IT READs THE FILENAME

fileName = fileName.trim();

if (fileName.length() <= 0)
{
System.out.println("\n\n WARNING: Since no file has been entered the program" +
" will now exit ");
System.exit(0);
} // if


return fileName;

} // getFileName



public AttendeesInfo loadAttendeeFile (String fileName)
{
/* Method: loadAttendeeFile
Description: This function loads the file named fileName
into the computers memory
Pre-condition:
Post-condition: Data is loaded into memory if file exists */


AttendeesInfo attInfo = new AttendeesInfo();
LinkedList list = new LinkedList();

try
{
File file = new File(fileName);
if( file.exists() )
{
FileReader filedr = new FileReader ( file );
BufferedReader inFile = new BufferedReader ( filedr );

// Declare variables to hold data from file

String attName, attAddress, line;
int attId, listSize;
listSize = 0;


// Reads the file


while( (line = inFile.readLine()) != null )
{
attName = line;
attId = new Integer(inFile.readLine().trim()).intValue();
attAddress = inFile.readLine();

// Create an object to store the attendee details
attInfo = new AttendeesInfo(attName, attId, attAddress);

list.addFirst(attInfo); //insert each attendee into the list.
listSize++;



} // while

inFile.close();

} // if
else
{
System.err.println("Attendee info file does not exist ! \n");
System.exit(0);
} // else

} // try

catch ( FileNotFoundException e )
{
System.err.println(e.getMessage());
System.exit(0);

} // catch
catch( IOException e)
{
System.err.println(e.getMessage());
System.exit(0);
} // catch


return attInfo;

} // loadAttendeeFile

} // FileInput

//==================================================
//Here is my attendee's info class part of the code

AttendeesInfo ()
{
/* Method: AttendeesInfo
Description: This is constructor for AttendeesInfo class
Pre-condition:
Post-condition: Private variables are
initialized */

attendee = " ";
idNumber = 0;
address = " ";

} // AttendeesInfo

Topic: When to use XML and Java Previous Topic   Next Topic Topic: JEditorpane - Wrapping

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use