Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Java Iterator Help, I need help with a method using iterator
sonesay
post 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 sad.gif


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
Go to the top of the page
 
+Quote Post
iGuest
post 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
Go to the top of the page
 
+Quote Post
apacheNewbie
post Feb 17 2008, 02:29 PM
Post #3


Newbie [Level 2]
**

Group: Members
Posts: 30
Joined: 23-November 06
Member No.: 33,877



QUOTE(FeedBacker @ Feb 8 2008, 01:36 PM) *
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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Java editor(72)
  2. Auto Run Java Program(11)
  3. Java Script Drop Down Menu With Css(2)
  4. [help] Java Script: Window.open(8)
  5. Java Mobile Applications(7)
  6. How To Create Java Button Or Frame(14)
  7. Java Basic Program Guidance(6)
  8. Java Helpdesk(8)
  9. Problem With Java Script Popup Form(1)
  10. The Power Of Java(14)
  11. Jsp Or Java Chat Script Like Mig33(4)
  12. Which Is The Best Java Web Framework?(0)
  13. Helpful Registry Edit For Java Programmers(2)
  14. Java Exercise Help(2)
  15. Beginning Java(10)
  1. Java In A High School Software Design Course(5)
  2. Java Part 1(2)
  3. Circular Movment/animation In Java?(6)
  4. Java Applet Query(1)
  5. Java Or C++(9)
  6. Java Vs Javascript(11)
  7. Java Object[][] Help(2)
  8. Learn Java Programming Language Online Step By Step(1)
  9. Java Game(2)
  10. Call Pdf995 From Java(0)
  11. How To Implement Single Instance Application On Java(0)
  12. Java Multithreading Issues(2)
  13. Java And Xml: Links You Must Have(1)


 



- Lo-Fi Version Time is now: 6th September 2008 - 06:28 PM