|
|
|
|
![]() ![]() |
Jan 22 2008, 08:01 AM
Post
#1
|
|
|
|||[ n00b King ]||| ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 681 Joined: 20-June 07 From: Auckland Member No.: 45,102 |
CODE /** * 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 Club A club holds memberships Membership 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. This post has been edited by sonesay: Jan 22 2008, 08:19 AM |
|
|
|
Feb 8 2008, 12:36 PM
Post
#2
|
|
|
Trap Double Mocha Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 2,360 Joined: 21-September 07 Member No.: 50,369 |
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 |
|
|
|
Feb 17 2008, 02:29 PM
Post
#3
|
|
|
Newbie [Level 2] ![]() ![]() Group: Members Posts: 30 Joined: 23-November 06 Member No.: 33,877 |
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 CODE String buffer;
str = ""; buffer = inputFile.readLine(); while (buffer != null) { str += buffer; buffer = inputFile.readLine(); } System.out.println(str); This post has been edited by apacheNewbie: Feb 17 2008, 02:29 PM |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 6th September 2008 - 06:28 PM |