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.
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...
Re: problems with for loop
Code :
for(int i=0; i < collection.size(); i++) {
Type item = collection.get(i);
...
}
What happened when you use this type of loop?
Re: problems with for loop
I can't make it work @Norm =/
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.
Re: problems with for loop
Code :
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;
}
}
}
}
Code :
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");
}
}
Code :
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)
Re: problems with for loop
Code :
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.