Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: problems with for loop

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems with for loop

    	case 3:
    		System.out.println("Hundens namn: ");
    	    String todelete=scan.nextLine();
     
             for (Dog h : dogs)
    	    	if (h.getname().equals(todelete))
                {
                    System.out.println(h);
    	    		dogs.remove(h);
    	    		System.out.println();
    	    		System.out.println(h+ " Är borttagen ");
                }
    	    break;


    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java: 343)
    at Register.main(Register.java:68)


    Thats the errormessage. The program runs fine except for when I want to delete an item it shows me the errormessage above. Whats wrong?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with for loop

    Did you read the API doc for the exception that is thrown?

    Try using a normal for loop to iterate through the collection.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with for loop

    Norm what do you mean, using a normal for loop? I have tryed but I cant seem to make this one work. Is the last piece of my assignment...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with for loop

    normal for loop?
     for(int i=0; i < collection.size(); i++) {
                 Type item = collection.get(i);
                 ...
              }

    What happened when you use this type of loop?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with for loop

    I can't make it work @Norm =/

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with for loop

    Make a small, simple program that compiles, executes and shows the problem.

    Post the full text of the error messages you are getting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with for loop

    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Iterator;
     
     
     
    public class Register{
    	public static void main(String []args){
     
    		double tailLength;
     
    		ArrayList<Dog> dogs = new ArrayList<Dog>();
     
    		for(;;){
    			System.out.println();
    			System.out.println("Make a choice:");
    			System.out.println("----------------------");
    			System.out.println();
    			System.out.println("1. Registrera new dog");
    			System.out.println("2. List the dogs");
    			System.out.println("3. Delete dog");
    			System.out.println("4. Exit");
     
    			Scanner keyboard= new Scanner(System.in);
     
    			int choice = Integer.parseInt (keyboard.nextLine());
     
    			switch (choice){
    			case 1:
    				System.out.println("Hundens namn: ");
    				String name=keyboard.nextLine();
     
    				System.out.println("Hundens vikt (kg): ");
    				int weight=Integer.parseInt(keyboard.nextLine());
     
    				System.out.println("Hundens Œlder: ");
    				int age=Integer.parseInt(keyboard.nextLine());
     
    				System.out.println("Ras: ");
    				String race=keyboard.nextLine();
     
    				tailLength=(double)age*weight/10;
    				if ("Tax".equalsIgnoreCase(race))
    					tailLength=3.7;
     
    				Dog dogx = new Dog(name,weight,age,race,tailLength); 
    				dogs.add(dogx);
    				System.out.println(dogx);
     
    				break;
     
    			case 2:
     
    				System.out.println("Ange svanslŠngd: ");
    				int minimumTailLength=Integer.parseInt(keyboard.nextLine());
    				for (int x=0; x<dogs.size(); x++){
    					tailLength = dogs.get(x).getTailLength();		
     
    					dogs.get(x);
    					if (tailLength >= minimumTailLength)
    						System.out.println(dogs.get(x));
    				}
    				break;
     
    			case 3:
    //See below!
     
     
    				break;
     
    			case 4: 
    			{
    				System.exit(0);
    			}
     
    			break;
     
    			default:
    			{
    				System.out.println("Fel val, try igen. ");
    			}
    			break;
    			}
    		}
     
     
    	}
    }

    public class Dog{
     
    	//Instansvariabler
    	private String name;
    	private int weight;
    	private int age;
    	private String race;
    	private double tailLength;
     
     
    	public Dog (String name, int weight, int age, String race, double tailLength){
    		//Variabler ges i konstruktorn
    		this.name=name;
    		this.weight=weight;
    		this.age=age;
    		this.race=race;
    		this.tailLength=tailLength;
     
    	}
     
    	public double getTailLength(){ 
    		return tailLength;
    	}
     
    	public String getName(){
    		return name;
    	}
     
    	public String toString(){
    		return ("Hunden "+name+" Šr en "+race+ " som vŠger: "+weight+ "Kg.\nDen Šr "+age+" "+"Œr gammal, "+"svanslŠngd: "+tailLength+" cm");
    	}
     
    }

                           case 3:
     
                                    System.out.println("Hundens namn: ");
                                String toDelete=keyboard.nextLine();
     
                             for (Dog deletedog : dogs)
                                    if (deletedog.getname().equals(toDelete))
                                {
                                            dogs.remove(deletedog);
                                            System.out.println();
                                            System.out.println(deletedog.getname() + " är borttagen");
                                }
                                            break;

    So thats the code entire program. 2 classes when pressing 3 you should be able to delete a dog from the register.
    when deleting I get this error message

    Errormessage:
    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:372)
    at java.util.AbstractList$Itr.next(AbstractList.java: 343)
    at Register.main(Register.java:71)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with for loop

    for (Dog deletedog : dogs)
    You are still using this syntax for the for loop. You need to use the syntax I suggested in post#4.


    The code you posted does NOT compile, execute and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 06:18 PM
  2. Java Scanners and loop problems
    By Studen2014 in forum Loops & Control Statements
    Replies: 2
    Last Post: September 10th, 2011, 08:27 AM
  3. Problems with incrementing/decrementing in a for loop..
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 13th, 2011, 05:15 AM
  4. [SOLVED] Problems with Loop Structure
    By Sean137 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 13th, 2010, 02:59 PM
  5. problems with loop in Java App
    By dmonx in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 04:13 PM