在main中使用引用变量调用方法

时间:2013-11-15 16:34:00

标签: java methods

我试图在包含其他方法的循环之外调用一个方法strList ArrayList变量,但是当我这样做时netbeans说它无法找到符号。我从其他方法调用方法就好了,但主要给我带来了问题。有什么我想念或根本不知道的东西?如果你这样做,谢谢你的帮助。

我的问题:writeList(strList);

程序

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        boolean shouldContinue = true;
        while (shouldContinue == true) {
            nameInput();
            Scanner input = new Scanner(System.in);
            shouldContinue = promptForContinue(input);
        }
writeList(strList);
    }

    /**
     *
     * @param name
     */
    public static void nameInput() throws FileNotFoundException, IOException {

        System.out.println("What is the name of the cartoon character : ");
        Scanner keyboard = new Scanner(System.in);
        CartoonStar star = new CartoonStar();
        String name = keyboard.next();
        star.setName(name);
        typeInput(keyboard, star);

    }

    public static void typeInput(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {

        System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n"
                + "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");

        boolean again = true;
        while (again) {

            try {
                input = keyboard.nextInt();

            } catch (Exception e) {
                System.out.println("Error :invalid input ");
                again = true;
                keyboard.next();
            }
            if (input > 0 && input <= 10) {
                again = false;
            }
        }

        switch (input) {
            case 1:
                star.setType(CartoonType.FOX);
                break;
            case 2:
                star.setType(CartoonType.CHICKEN);
                break;
            case 3:
                star.setType(CartoonType.RABBIT);
                break;
            case 4:
                star.setType(CartoonType.MOUSE);
                break;
            case 5:
                star.setType(CartoonType.DOG);
                break;
            case 6:
                star.setType(CartoonType.CAT);
                break;
            case 7:
                star.setType(CartoonType.BIRD);
                break;
            case 8:
                star.setType(CartoonType.FISH);
                break;
            case 9:
                star.setType(CartoonType.DUCK);
                break;
            case 10:
                star.setType(CartoonType.RAT);
                break;
        }
        popularityNumber(keyboard, star);
    }

    public static void popularityNumber(Scanner keyboard, CartoonStar star) throws FileNotFoundException, IOException {
        System.out.println("What is the cartoon popularity number?");

        boolean again = true;
        while (again) {
            try {
                popularity = keyboard.nextInt();
            } catch (Exception e) {
                System.out.println("Error : invalid input:");
                again = true;
                keyboard.next();

            }
            if (popularity >= 0 && popularity <= 10) {
                again = false;
            }

        }
        star.setPopularityIndex(popularity);
        ArrayList<Object> strList = new ArrayList<Object>();
        strList.add(star.getName());
        strList.add(star.getType());
        strList.add(star.getPopularityIndex());


        writeList(strList, keyboard);
    }

    public static void printList(ArrayList<Object> strList) {
        System.out.println(strList);
    }
    public static void writeList(ArrayList<Object> strList, Scanner keyboard) throws FileNotFoundException, IOException{
        System.out.println("Enter the file name");
        String fileName = keyboard.next();
        PrintWriter writer = new PrintWriter(fileName + ".txt");
        System.out.println("Saving. . . . ");
        System.out.println("Saved!");


    }
    //To change body of generated methods, choose Tools | Templates.

    private static boolean promptForContinue(final Scanner input) {
        boolean isValid = false;
        String userInput = "";
        do {
            System.out.print("Continue (Yes/No):");
            userInput = input.next();

            isValid = userInput.matches("Yes|No");
// if the input matches yes, ask for the required variables, else break. 
            if (!isValid) {
                System.out.println("\nInvalid entry.");
            }
        } while (!isValid);

        return userInput.equals("Yes") ? true : false;
    }

}

1 个答案:

答案 0 :(得分:1)

您的writeList方法有两个参数;你只提供一个。另外,我没有看到您将传递的变量strList定义为第一个参数。它是一个静态变量,在您未显示的代码中定义?

看到第二个参数应该是Scanner,也许这对你有用:

    Scanner input = new Scanner(System.in);
    while (shouldContinue == true) {
        nameInput();
        shouldContinue = promptForContinue(input);
    }
    writeList(strList, input);

将来您应该从编译器提供完整的错误消息。让人们猜出错误可能会产生什么效果。

相关问题