循环时无法继续输入参数以创建arraylist的对象

时间:2014-04-06 00:14:06

标签: java arraylist

编辑:我解决了。问题是我试图在错误的位置清除扫描仪中保存的先前值(需要在if语句之外)。我还使用switch变量清理了continueInput方法。我会将正确的版本作为答案发布。

我试图用循环创建对象,然后将它们存储在我以后打印到文件的arraylist中。我可以很好地创建第一个对象,但是在循环的第二次迭代中,我无法移动进入卡通名称。我得到了这个名字的提示,但无论我输入什么,都没有任何反应。

这是我的主要课程:

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
enum CartoonType{FOX,CHICKEN,RABBIT,MOUSE,DOG,CAT,BIRD,FISH,DUCK,RAT,NULL};

public class Main {


public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    ArrayList<CartoonStar> cartoonStars = new ArrayList<CartoonStar>();
    String cartoonName;
    CartoonType cartoonType;
    String cartoonTypeString;
    int popularityIndex;
    boolean moreInput;
    int counter = 0;
    do {
        cartoonName = cartoonName(stdin);
        cartoonType = cartoonType(stdin);
        cartoonTypeString = cartoonType.toString();
        popularityIndex = popularityNumber(stdin);
        cartoonStars.add(new CartoonStar(cartoonName, cartoonType, popularityIndex));
        cartoonStars.get(counter).setName(cartoonName);
        cartoonStars.get(counter).setType(cartoonType);
        cartoonStars.get(counter).setPopularityIndex(popularityIndex);
        moreInput = continueInput(stdin);
        if (moreInput) {
            counter++;
        }
    } while (moreInput);
}

public static String cartoonName(Scanner stdin) {
    String name;
    String name1;
    int a = 1;
    System.out.print("Please enter the name of the cartoon character: ");
    name = stdin.nextLine();
    do {
        name1 = name;
        for (int i = 0; i < name.length(); i++) {
            if (!Character.isLetter(name.charAt(i))) {
                System.out.println("Please ensure you've entered the correct name. Re-enter the name or enter 'continue' to proceed: ");
                name = stdin.nextLine();
                if (name.equalsIgnoreCase("continue")) {
                    a = 0;
                    name = name1;
                } else {
                    a = 1;
                }
            } else {
                a = 0;
            }
        }
    } while (a == 1);
    return name;
} // end of cartoonName method

public static CartoonType cartoonType(Scanner stdin) {
    int cartoonType = 0;
    CartoonType cType = CartoonType.valueOf("NULL");
    int a;
    do {
        try {
            System.out.print("Please enter a number corresponding to the type of cartoon character below.\n"
                    + "1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT\n"
                    + "Please enter the type of cartoon character: ");
            cartoonType = stdin.nextInt();
            while (cartoonType < 1 || cartoonType > 10) {
                System.out.print("Sorry, please enter a number from 1 to 10. The corresponding types are:/n"
                        + "1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT\n");
                cartoonType = stdin.nextInt();
            } 
            a = 0;
        } catch (InputMismatchException ex) {
            System.out.print("Sorry, please enter only integer values.");
            a = 1; 
            stdin.nextLine(); // drop last input
        }
    } while (a == 1);
    switch (cartoonType) {
        case 1:
            cType = CartoonType.valueOf("FOX");
            break;
        case 2:
            cType = CartoonType.valueOf("CHICKEN");
            break;
        case 3:
            cType = CartoonType.valueOf("RABBIT");
            break;
        case 4:
            cType = CartoonType.valueOf("MOUSE");
            break;
        case 5:
            cType = CartoonType.valueOf("DOG");
            break;
        case 6:
            cType = CartoonType.valueOf("CAT");
            break;
        case 7:
            cType = CartoonType.valueOf("BIRD");
            break;
        case 8:
            cType = CartoonType.valueOf("FISH");
            break;
        case 9:
            cType = CartoonType.valueOf("DUCK");
            break;
        case 10:
            cType = CartoonType.valueOf("RAT");
            break;
    }
    return cType;
} // end of cartoonType method


public static int popularityNumber(Scanner stdin) {
    int popNum = 0;
    int a;
    do {
        try {
            System.out.print("Please enter a cartoon popularity number from 1 to 10, with 10 being the highest: ");
            popNum = stdin.nextInt();
            while (popNum < 1 || popNum > 10) {
                System.out.print("Sorry, please enter a value from 1 to 10: ");
                popNum = stdin.nextInt();
            }
            a = 1;
        } catch (InputMismatchException ex) {
            a = 0;
            System.out.print("Sorry, please enter only integer values.");
            stdin.next();
        }
    } while (a == 0);
    return popNum;
} // end of popularityNumber method

public static boolean continueInput(Scanner stdin) {
    boolean moreInput = false;
    int a = 1;
    System.out.print("More input? Enter 'yes' for more input, or 'no' to quit: ");
    String input = stdin.next();
    do {
        for (int i = 0; i < input.length(); i++) {
            if (!Character.isLetter(input.charAt(i))) {
                System.out.println("Please enter only 'yes' or 'no'.");
                stdin.next();
                a = 1;
            } else if (input.equalsIgnoreCase("yes")) {
                a = 0;
                moreInput = true;
            } else if (input.equalsIgnoreCase("no")) {
                a = 0;
                moreInput = false;
            } else {
                a = 1;
                System.out.print("Please enter only 'yes' or 'no'.");
                stdin.next();
            }
        }
    } while (a == 1);
    return moreInput;
}

}

这是我的另一堂课:

public class CartoonStar {

 private String name;   
 private CartoonType type;   //enum types
 private int popularityIndex;  //1 to 10 10 being the most popular

 public CartoonStar() {
 }//end no argument construtor

 public CartoonStar(String name,CartoonType type, int popularityIndex) {
   setName(name);
   setType(type);
   setPopularityIndex(popularityIndex);
 }//end full constructor

 //getters and setters
 public void setName(String name) {
   this.name = name;
 }      
 public String getName() {
   return name;
 }
 public void setType(CartoonType type) {
   this.type = type;
 }
 public CartoonType getType() {
   return type;
 }
 public void setPopularityIndex(int popularityIndex){
   this.popularityIndex = popularityIndex;
 }
 public int getPopularityIndex(){
   return popularityIndex;
 }  
}//end of class CartoonStar

这是我的代码的示例输出:

Please enter the name of the cartoon character: w

Please enter a number corresponding to the type of cartoon character below.

1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,

6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT

Please enter the type of cartoon character: 2

Please enter a cartoon popularity number from 1 to 10, with 10 being the highest: 2

More input? Enter 'yes' for more input, or 'no' to quit: yes

Please enter the name of the cartoon character: w

BUILD STOPPED (total time: 11 seconds)

此时我停止了该计划,因为它并没有让我超越这一点。我的main方法中的循环问题是什么?我没有在没有明确命名的情况下制作对象,所以我对如何创建它们感到困惑,以后能够从我的arraylist中回忆它们。

2 个答案:

答案 0 :(得分:0)

无限循环,

您需要将用户输入的新值分配给continueInput

中的输入变量

喜欢input = stdin.next();

所以将其改为以下

public static boolean continueInput(Scanner stdin) {
    boolean moreInput = false;
    int a = 1;
    System.out.print("More input? Enter 'yes' for more input, or 'no' to quit: ");
    String input = stdin.next();
    do {
        for (int i = 0; i < input.length(); i++) {
            if (!Character.isLetter(input.charAt(i))) {
                System.out.println("Please enter only 'yes' or 'no'.");
                input = stdin.next();
                a = 1;
            } else if (input.equalsIgnoreCase("yes")) {
                a = 0;
                moreInput = true;
            } else if (input.equalsIgnoreCase("no")) {
                a = 0;
                moreInput = false;
            } else {
                a = 1;
                System.out.print("Please enter only 'yes' or 'no'.");
                input = stdin.next();
            }
        }
    } while (a == 1);
    return moreInput;
}

答案 1 :(得分:0)

以下是已更正的主要课程(卡通星级课程未更改):

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    ArrayList<CartoonStar> cartoonStars = new ArrayList<CartoonStar>();
    String cartoonName;
    CartoonType cartoonType;
    String cartoonTypeString;
    int popularityIndex;
    boolean moreInput;
    int counter = 0;
    do {
        cartoonName = cartoonName(stdin);
        cartoonType = cartoonType(stdin);
        cartoonTypeString = cartoonType.toString();
        popularityIndex = popularityNumber(stdin);
        cartoonStars.add(new CartoonStar(cartoonName, cartoonType, popularityIndex));
        cartoonStars.get(counter).setName(cartoonName);
        cartoonStars.get(counter).setType(cartoonType);
        cartoonStars.get(counter).setPopularityIndex(popularityIndex);
        moreInput = continueInput(stdin);
        counter++;
    } while (moreInput == true);
} // end of main method

public static String cartoonName(Scanner stdin) {
    String name;
    String name1;
    int a = 1;
    System.out.print("Please enter the name of the cartoon character: ");
    name = stdin.nextLine();
    do {
        name1 = name;
        for (int i = 0; i < name.length(); i++) {
            if (!Character.isLetter(name.charAt(i))) {
                System.out.println("Please ensure you've entered the correct name. Re-enter the name or enter 'continue' to proceed: ");
                name = stdin.nextLine();
                if (name.equalsIgnoreCase("continue")) {
                    a = 0;
                    name = name1;
                } else {
                    a = 1;
                }
            } else {
                a = 0;
            }
        }
    } while (a == 1);
    return name;
} // end of cartoonName method

public static CartoonType cartoonType(Scanner stdin) {
    int cartoonType = 0;
    CartoonType cType = CartoonType.valueOf("NULL");
    int a;
    do {
        try {
            System.out.print("Please enter a number corresponding to the type of cartoon character below.\n"
                    + "1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT\n"
                    + "Please enter the type of cartoon character: ");
            cartoonType = stdin.nextInt();
            while (cartoonType < 1 || cartoonType > 10) {
                System.out.print("Sorry, please enter a number from 1 to 10. The corresponding types are:/n"
                        + "1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT\n");
                cartoonType = stdin.nextInt();
            } 
            a = 0;
        } catch (InputMismatchException ex) {
            System.out.print("Sorry, please enter only integer values.");
            a = 1; 
            stdin.nextLine(); // drop last input
        }
    } while (a == 1);
    switch (cartoonType) {
        case 1:
            cType = CartoonType.valueOf("FOX");
            break;
        case 2:
            cType = CartoonType.valueOf("CHICKEN");
            break;
        case 3:
            cType = CartoonType.valueOf("RABBIT");
            break;
        case 4:
            cType = CartoonType.valueOf("MOUSE");
            break;
        case 5:
            cType = CartoonType.valueOf("DOG");
            break;
        case 6:
            cType = CartoonType.valueOf("CAT");
            break;
        case 7:
            cType = CartoonType.valueOf("BIRD");
            break;
        case 8:
            cType = CartoonType.valueOf("FISH");
            break;
        case 9:
            cType = CartoonType.valueOf("DUCK");
            break;
        case 10:
            cType = CartoonType.valueOf("RAT");
            break;
    }
    return cType;
} // end of cartoonType method


public static int popularityNumber(Scanner stdin) {
    int popNum = 0;
    int a;
    do {
        try {
            System.out.print("Please enter a cartoon popularity number from 1 to 10, with 10 being the highest: ");
            popNum = stdin.nextInt();
            while (popNum < 1 || popNum > 10) {
                System.out.print("Sorry, please enter a value from 1 to 10: ");
                popNum = stdin.nextInt();
            }
            a = 1;
        } catch (InputMismatchException ex) {
            a = 0;
            System.out.print("Sorry, please enter only integer values.");
            stdin.next();
        }
    } while (a == 0);
    return popNum;
} // end of popularityNumber method

public static boolean continueInput(Scanner stdin) {
    boolean moreInput = false;
    int a;
    System.out.print("More input? Enter 'yes' for more input, or 'no' to quit: ");
    String input;
    do {
        switch (input = stdin.next().toLowerCase()) {
            case "yes":
                a = 0;
                moreInput = true;
                break;
            case "no":
                a = 0;
                moreInput = false;
                break;
            default:
                a = 1;
                System.out.print("Please enter only 'yes' or 'no'.");
        }
        stdin.nextLine();
    } while (a == 1);
    return moreInput;
} // end of continueInput method

}