简体   繁体   中英

Calling methods with reference variables in main

I am trying to call a method the the strList ArrayList variable in my main outside of a loop that contains other methods but when I do so netbeans says it cannot find the symbol. I have called methods from other methods just fine but the main gives me problems. Is there something I am missing or simply do not know? Thank you for the help if you do.

ex of my issue: writeList(strList);

Program

    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;
    }

}

Your writeList method takes two arguments; you provide only one. Also, I don't see where you define the variable strList that you pass as the first argument. Is it a static variable, defined in code you have not shown?

Seeing that the second argument should be a Scanner , maybe this will work for you:

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

In the future you should provide the full error message from the compiler. Making people guess what the error might be is not productive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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