在之前下項目的時候遇到這個.NoSuchElementException異常,當時我寫到一個大類的結尾。但是編譯器。從未報錯。然而在運行的時候出現了這樣的異常,非常頭疼 ,一到運行時候就報異常,我就上網搜索了一下,才明白,這是我的剛開始寫的代碼,下面寫出這個錯誤的例子及分析,希望能及時的幫助和我同樣犯了小錯誤的菜鳥!
package com.PETProgram;
import java.util.Scanner;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
import java.util.ArrayList;
import static java.lang.System.out;
public class PetSystemLogin {
?public static Scanner input = new Scanner(System.in);
??????
?????? Pet p1 = new Pet("花花");
?public static void main(String[] args) {
????? out.println("寵物商店正在啟動");
????? out.println("Wonder land醒來,所有寵物從oracle中醒來");
????? out.println("*********************************");
????? out.println("寵物商店正在啟動");
???? ArrayList <Pet>list = new ArrayList<Pet>();
????? list.add(new Pet("花花"));
????? list.add(new Pet("貝貝"));
????? list.add(new Pet("成成"));
????? list.add(new Pet("露露"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("1"));
????? list.add(new Pet("shizi"));
????? Iterator<Pet> it = list.iterator();
????? while(it.hasNext())
????? {?? int i = 0;
??? ?
????? for(;i<list.size();i++)
??? ?? {
??? ?? System.out.println("第"+(i+1)+"個"+"寵物,"+"名字叫"+((Pet)it.next()).name);
??? ??
??? ?? }
????? }
???
?
????? out.println("********************************");
????? out.println("所有寵物主人從oracle中醒來");
????? out.println("********************************");
????? ArrayList<PetOwner> list1 = new ArrayList<PetOwner>();
??????? PetOwner pet = new PetOwner("小明");
??????? list1.add(pet);
??????? list1.add(new PetOwner("小強"));
??????? Iterator<PetOwner> it1 = list1.iterator();
??????? while(it1.hasNext())
??????? {?? int i = 0;
????? ?? for(;i<list1.size();i++)
????? ?? {
????? ?? System.out.println("第"+(i+1)+"個"+"寵物主人,"+"名字叫"+((PetOwner)it1.next()).name);
????? ?? }
?????? }
??????? out.print("所有寵物商店從oracle中醒來");
??????? out.print("************************************\n");
??????? ArrayList<PetStore> list2 = new ArrayList<PetStore>();
??????? PetStore pets = new PetStore("北京西苑");
??????? list2.add(pets);
??????? list2.add(new PetStore("重慶觀音橋"));
??????? Iterator<PetStore> it2 = list2.iterator();
??????? while(it2.hasNext())//錯誤在這里,原來的迭代的元素還是寫在還在迭代上面的元素,其實應該迭代現在是it2
??????? {
??????? ? out.println("我的名字叫:"+(it2.next()).Storename);
??????? }
??????
??????? out.println("請選擇登陸模式,輸入1為寵物主人登陸,輸入2為寵物商店登陸");
??????? int choice = input.nextInt();
??????? if(choice ==1)
??????? {
??????? ?pet.OwnerLogin();
??????? ?pet.isAccount();
??????? }else if(choice ==2)
??????? {
??????? ?pets.StoreLogin();
??????? }else if(choice==0)
??????? {
??????? ?System.exit(0);
??????? }
???????
?}
???
?
}
?
?元素迭代錯誤的錯誤造成的,其實
?
public class PetSystemLogin {
?public static Scanner input = new Scanner(System.in);
??????
?????? Pet p1 = new Pet("花花");
?public static void main(String[] args) {
????? out.println("寵物商店正在啟動");
????? out.println("Wonder land醒來,所有寵物從oracle中醒來");
????? out.println("*********************************");
????? out.println("寵物商店正在啟動");
???? ArrayList <Pet>list = new ArrayList<Pet>();
????? list.add(new Pet("花花"));
????? list.add(new Pet("貝貝"));
????? list.add(new Pet("成成"));
????? list.add(new Pet("露露"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("老虎"));
????? list.add(new Pet("1"));
????? list.add(new Pet("shizi"));
????? Iterator<Pet> it = list.iterator();
????? while(it.hasNext())
????? {?? int i = 0;
??? ?
????? for(;i<list.size();i++)
??? ?? {
??? ?? System.out.println("第"+(i+1)+"個"+"寵物,"+"名字叫"+((Pet)it.next()).name);
??? ??
??? ?? }
????? }
???
?
????? out.println("********************************");
????? out.println("所有寵物主人從oracle中醒來");
????? out.println("********************************");
????? ArrayList<PetOwner> list1 = new ArrayList<PetOwner>();
??????? PetOwner pet = new PetOwner("小明");
??????? list1.add(pet);
??????? list1.add(new PetOwner("小強"));
??????? Iterator<PetOwner> it1 = list1.iterator();
??????? while(it1.hasNext())
??????? {?? int i = 0;
????? ?? for(;i<list1.size();i++)
????? ?? {
????? ?? System.out.println("第"+(i+1)+"個"+"寵物主人,"+"名字叫"+((PetOwner)it1.next()).name);
????? ?? }
?????? }
??????? out.print("所有寵物商店從oracle中醒來");
??????? out.print("************************************\n");
??????? ArrayList<PetStore> list2 = new ArrayList<PetStore>();
??????? PetStore pets = new PetStore("北京西苑");
??????? list2.add(pets);
??????? list2.add(new PetStore("重慶觀音橋"));
??????? Iterator<PetStore> it2 = list2.iterator();
??????? while(it2.hasNext())//錯誤在這里,原來的迭代的元素還是寫在還在迭代上面的元素,其實應該迭代現在是it2
??????? {
??????? ? out.println("我的名字叫:"+(it2.next()).Storename);
??????? }
??????
??????? out.println("請選擇登陸模式,輸入1為寵物主人登陸,輸入2為寵物商店登陸");
??????? int choice = input.nextInt();
??????? if(choice ==1)
??????? {
??????? ?pet.OwnerLogin();
??????? ?pet.isAccount();
??????? }else if(choice ==2)
??????? {
??????? ?pets.StoreLogin();
??????? }else if(choice==0)
??????? {
??????? ?System.exit(0);
??????? }
???????
?}
???
?
}
?
?元素迭代錯誤的錯誤造成的,其實
?