Jump to content
xisto Community
Sign in to follow this  
sonesay

Java Iterator Help I need help with a method using iterator

Recommended Posts

/**     * Remove from the club's collection all members who     * joined in the given month, and return them stored     * in a sparate collection object.     * @param month The month of the membership.     * @param year The year of the membership.     * @return The number of members.     */    public ArrayList<Membership> purge(int month, int year)    {               if(month >= 1 && month <= 12)        {             Iterator it = members.iterator();             ArrayList<Membership> purged;             purged = new ArrayList();             int counter = 0;                  while(it.hasNext())                 {                    Membership member = (Membership) it.next();                    if((member.getMonth() == month) && (member.getYear() == year))                    {                       purged.add(member);                       members.remove(counter);                    }                    counter++;                }                return purged;        }        System.out.println("Invalid month of "+month);        return null;    }

This is what I've got so far. there is bascially two classes involved here:Membership and ClubA club holds membershipsMembership holds details of name, join month, join year.The method is suppose to take a month and year then look through the clubs membership collection and return the ones at match as an array of Membership. THe ones that match also need to be removed from the clubs collection. I think whats happening while I'm observing in the debugger is when It loops through the ones that match and start removing it messes up the iterator so no more loops happen and only 1 match is return. Any ideas on how to go about this? I'm still fairly new to java :)edit:Ok I looked iterator method remove() ; ; that did the job. Sorry guys for posting this up it was my own fault. I read this method first round but the description didnt sound right. I thought it was gonna remove the whole iterator. Probably a good idea to delete this Mod since I dont think its gonna be any valuable.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

FileLetterCounter

Java Iterator Help

 

/**Can anyone please help me with this.Exception in thread "main" java.Lang.NullPointerException at FileLetterCounter.Main(FileLetterCounter.Java:41)*/

 

Import java.Util.Scanner;

Import java.Io.*;

 

public class FileLetterCounter

{

public static void main(String[] args) throws IOException

{

Scanner keyboard = new Scanner(System.In);

 

String str;

String fileName;

int charCount = 0;

 

// get users filename

System.Out.Print("Please enter the name of the file: ");

fileName = keyboard.NextLine();

 

FileReader freader = new FileReader(fileName);

 

BufferedReader inputFile = new BufferedReader(freader);

 

// get users character

System.Out.Print("Please enter a character: ");

char userChar = keyboard.NextLine().CharAt(0);

 

// tell the user what the program does

System.Out.Println("and****This program will return how many times" +

" the character you entered showed up in" +

" the file you entered.****");

str = inputFile.ReadLine();

 

while (str != null)

{

System.Out.Println(str);

str = inputFile.ReadLine();

}

for(int I = 0; I <str.Length(); I++)

 

if (str.CharAt(I) == userChar)

{

charCount++;

}

System.Out.Println("nThe specified character " +""" + userChar +

"" is inside the inputFile: " + inputFile +

" " + charCount + " times.and");

inputFile.Close();

}

}

 

-reply by georg johansen

Share this post


Link to post
Share on other sites

while (str != null) {
System.Out.Println(str);
str = inputFile.ReadLine();
}

// str == null


After this while loop, your str is null

Therefore the next line " for(int I = 0; I <str.Length(); I++)" throws exception.

May be you need to add another variable as buffer before adding the line to str variable.

example
String buffer;		str = "";		buffer = inputFile.readLine();		while (buffer != null)		{			str += buffer;				buffer = inputFile.readLine();		}		System.out.println(str);

Edited by apacheNewbie (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.