Monday, April 23, 2012

Reading a file into a list?

I am trying to read a file into a list, and take every other line from this list, and try to add this to the hashset, and if the result of this operation is false, output the duplicate string. For some reason, it is not working, and i'm not sure what i've done wrong. Thanks .. here is my code:



public class Duplicates
{
public static void main(String[] args)
{

PrintWriter output = null;
BufferedReader input = null;

try
{

input = new BufferedReader(new FileReader(args[0]));
output = new PrintWriter(new FileWriter(args[1]));

List<String> list = new ArrayList<String>();
Set<String> usr = new HashSet<String>();

//Read the lines into lineList
String current;
boolean result;
while ((current = input.readLine()) != null)
list.add(current);


for (int index = 0; index < lineList.size() - 1; index =+2)
{
result = usr.add(list.get(index));
if (result = true)
System.out.println(list.get(index));
}
}//try
catch (Exception exception)
{
System.err.println(exception);
}//catch
finally
{
try {if (input != null) input.close(); }
catch (IOException exception)
{ System.err.println( exception) }
}
}
}




No comments:

Post a Comment