繁体   English   中英

在main中具有引用变量的调用方法

[英]Calling methods with reference variables in main

我试图在包含其他方法的循环之外的主循环中调用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;
    }

}

您的writeList方法writeList两个参数; 您只提供一个。 另外,我看不到在哪里定义作为第一个参数传递的变量strList 它是一个静态变量,由您未显示的代码定义吗?

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

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

将来,您应该提供编译器提供的完整错误消息。 让人们猜测错误可能是徒劳的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM