rosie_hyde
rosie_hyde

Reputation: 133

How to get a system.out.println to return in a method

I have a program which plays a game of superhero top trumps and I need to repeat the menu every time a user plays the game. I've tried to create a method (displayMenus) which would be inserted into every switch statement (except for closing the program), however I don't understand how to get it to return this menu.

public class Heros {

public static void main(String[] args) {



Scanner input = new Scanner(System.in);

int index = 0;


    do {

     int inp = input.nextInt();

    switch (inp) {
        //FIRST
        case 1:
            index = 0;
            System.out.println(HerosAsList.getHeros().get(index));
            System.out.println(displayMenus);
            break;
        //NEXT
        case 2:
            index++;
            if (index > HerosAsList.getHeros().size() - 1) {
                index = HerosAsList.getHeros().size() - 1;
            }
            System.out.println(HerosAsList.getHeros().get(index));
            System.out.println(displayMenus);
            break;
        //PREV
        case 3:
            index--;
            if (index < 0) {
                index = 0;
            }
            System.out.println(HerosAsList.getHeros().get(index));
            System.out.println(displayMenus);
            break;
        //LAST
        case 4:
            index = HerosAsList.getHeros().size() - 1;
            System.out.println(HerosAsList.getHeros().get(index));
            System.out.println(displayMenus);
            break;
        //QUIT
        case 0:
            System.out.println("Closing system");
            System.exit(inp);

            break;
    }
}

while(index<HerosAsList.getHeros ().size());

}  
public static displayMenus () {


    System.out.println("First - 1");
    System.out.println("Next -  2");
    System.out.println("Prev -  3");
    System.out.println("Last -  4");
    System.out.println("Quit -  5");
    System.out.println("");
    System.out.println("Enter Choice:");



}
}

any help is greatly appreciated, I just can't seem to get my head around methods!

Upvotes: 1

Views: 17287

Answers (3)

Andy Turner
Andy Turner

Reputation: 140319

Firstly, the declaration of displayMenus is invalid. You need to give it a return type (even if that is void), e.g.:

public static void displayMenus () {

If you make it void, you would need to invoke displayMenus() without calling System.out.println:

displayMenus();

If you make it String, you would need to build a String in displayMenus and return it, e.g.:

public static String displayMenus() {
  return "First - 1" + ...;
}

then you can invoke System.out.println(displayMenus()).

Upvotes: 2

Mike Elofson
Mike Elofson

Reputation: 2037

I simplified the code down, because there were quite a few syntax errors in the code itself, but this should answer your question. Your displayMenus() function doesn't need to return a String, you can instead call this at the beginning of your do...while loop. The logic flow should be:

  • Initialize scanner
  • Create variable for choice
  • Enter do...while loop
  • Call displayMenus()
  • Read user choice from scanner
  • Do switch logic
  • Back to 3rd item if the loop isn't broken

I made a small class to demo this:

package zzzTestProj;

import java.util.Scanner;

public class NewClass {

    public static void main(String[] args) {

        Scanner userInputScanner = new Scanner(System.in);

        int userMenuChoice;

        do {
            displayMenus();
            userMenuChoice = userInputScanner.nextInt();

            switch (userMenuChoice) {
            // FIRST
            case 1:
                break;
            // NEXT
            case 2:
                break;
            case 0:
                System.out.println("Closing system");
                System.exit(userMenuChoice);

                break;
            }
        } while (userMenuChoice != 5);

    }

    public static void displayMenus() {

        System.out.println("First - 1");
        System.out.println("Next -  2");
        System.out.println("Prev -  3");
        System.out.println("Last -  4");
        System.out.println("Quit -  5");
        System.out.println("");
        System.out.print("Enter Choice:");

    }
}

Upvotes: 1

jonk
jonk

Reputation: 1494

Just change displayMenus to return the menu as a String, rather than printing it:

public static String displayMenus() {
    return "First - 1\nNext -  2\nPrev -  3\nLast -  4\nQuit -  5\n\nEnter Choice:\n";
}

Upvotes: 1

Related Questions