Skip to content
Advertisement

APCSP create task issue – Method ends program after being called [closed]

I am creating a short RPG game for my AP CSP project and for some reason when I call the method Element in line 310-313, it just ends the rest of the code in Main (which is all the remaining code in the program). The user is required to press x to continue the game but it skips all of that and auto-fills the user-inputs correctly. Put it short, once you select your element in the code, the program finishes the game by itself, which is not supposed to happen since the user needs to have its input to continue the dialogue.

Aforementioned, the intended output of this code is to complete the dialogue with the user input and user information only. Please help as this is due soon!

    import java.util.*;
import java.lang.*;
/**
This class is the "command and return center" for the program. Essentialy,
the main outputs and some inputs occur here.
 */
public class Main
{
    private static String elemental;
    private static void Element(String e)
    {
        Scanner scan = new Scanner (System.in);
        System.out.println("Which one do you pick? Type the following letters to obtain your element.");
        System.out.println("f - Fire");
        System.out.println("w - Water");
        System.out.println("a - Air");
        System.out.println("e - Earth");
        System.out.println("Type your letter now!");
        e = scan.nextLine();
        elemental = e;
        if (e.charAt(0) == 'f')
        {
            e = "Fire";
        }
        if (e.charAt(0) == 'w')
        {
            e = "Water";
        }
        if (e.charAt(0) == 'a')
        {
            e = "Air";
        }
        if (e.charAt(0) == 'e')
        {
            e = "Earth";
        }
        System.out.println(e + ", nice pick!");
    }   
    public static void main (String args[]) 
    {
        Scanner scan = new Scanner (System.in);
        int c = 0;
        int trainc = 0;
        ArrayList<String> hits = new ArrayList<>();
        hits.add("5");
        hits.add("4");
        hits.add("3");
        hits.add("2");
        hits.add("1");
        int lhits = 0;
        System.out.println("What is your name?");
        String name = scan.nextLine();
        System.out.println("Press x to start");
        String play = scan.nextLine();
        String e = "";
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING* This drag-a-agon is to powerful for m-e.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*Dragon Laughs*");
            System.out.println("");
            System.out.println("Press x to attack the dragon");
        } 
        hits.remove("5");
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("I have dealt no damage, even with my elemental buffs. Even with all the rare potions, I cannot deal the damage I want to the dragon."); 
            System.out.println("");
            System.out.println("Press x to attack the dragon");
        }  
        hits.remove("4");
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("I have to retreat, it looks like I am on the blind spot of the dragon. I have to go. NOW OR NEVER!!!");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*Gets out of Bed*");
            System.out.println("*HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING*");
            System.out.println("I-I'm back.");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("");
            System.out.println("");
            System.out.println("--*RESTING ARC*--");
            System.out.println("");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("I forgot that I came home and rested, and...lost to that dragon.");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Huh? What is this lying in front of my door? A letter? From the-THE KING?!?!?!?!");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Why couldn't I have a nice morning *Sigh*");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Well, I guess I have to open it and found out what it says.");
            System.out.println("");
            System.out.println("Press x to interact with the letter");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dear " + name + ", I have recently gotten news on your unsuccessful mission on defeating the dragon.");
            System.out.println("I am dissapointed, and you will be prompted to confront my prescence this afternoon, for your journey could not have been more upsetting and shameful to this nation.");
            System.out.println("");
            System.out.println("Press x to continue");
        } 
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Another day, another quarell with the King. Why can't we just get along. *Sigh*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Wait...the- THE AFTERNOON!?! I have to get ready NOW!");
            System.out.println("What time is it...11 pm, 11 PM!?!?!? oh brother...");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*1 Hour Passes and the Noble Knight is at the King's Door Guarded by Knights*"); 
            System.out.println("Heya Guards! Wanna open this giant red door for your ol' pal " + name +"?");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*Knock**Knock*");
            System.out.println("*Large Cushioned Door Opens*");
            System.out.println("");
            System.out.println("");
            System.out.println("--*King's Arc*--");
            System.out.println("");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*Mrmmr's Across The Room Became Silent*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: " + name + ", come in and walk forth in front of my prescence.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println(name + ": Kk, whatever you say Jim.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("*Mrmmr's Across The Room Get Louder.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: YOUR MAJESTY!! CALL ME YOUR MAJESTY!! You should know your place...");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println(name + ": Okayyyy... calm down... Your Majesty...");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: DONT TELL THE KING WHAT TO DO!!! ONLY THE KING CAN TELL WHAT THE KING NEEDS TO DO!!! NOT A KNIGHT!");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x')
        {
            System.out.println(name + ":You just threw Democracy out the window, huh.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: You lost to the dragon, how will I know that you are ready to fight again? I can only trust you since you have the most experience fighting alone.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println(name + ": I will train, just, just give me time, I will defeat this dragon.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: Okay, come back to me when you are ready.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("");
            System.out.println("");
            System.out.println("--*Training Arc*--");
            System.out.println("");
            System.out.println("");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("I have to train, no matter how long or hard it takes, I will defeat this dragon.");
            System.out.println("");
        }
        System.out.println("Press x as many times you want, this number will equal your years spent training.");
        System.out.println("To stop training, type s");
        System.out.println("Go! Your training has already begun!");
        String stop = "x";
        while(stop.charAt(0) != 's')
        {
            trainc++;
            System.out.println("You have trained for " + trainc + " years.");
            System.out.println("Press x to continue training or s to stop.");
            stop = scan.nextLine();
        }
        System.out.println("King: How long has it been?");
        System.out.println("");
        System.out.println("Press x to continue");
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println(name + ": " + trainc + " years, boss man.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("");
            System.out.println("");
            System.out.println("--*Merchant Arc*--");
            System.out.println("");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("King: Hey, someone call that merchant, we are getting you some armor.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        play = scan.nextLine();
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Merchant: Howdy! I have 4 elemental swords: Fire, Water, Air, and Earth. Pick one, and your super finisher ability will be based off of the element of your choice! Note that it takes 5 attacks to charge the ability!");
            System.out.println("");
            Element(e);
        }
        
        System.out.println("Press x to continue");
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Merchant: Since you attacked the dragon before, your charge would be less than 5, how many times did you hit the dragon?");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        lhits = hits.size();
        if (play.charAt(0) == 'x' )
        {
            System.out.println(name + ": 2 times, meaning that my charge count is " + lhits + ".");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Merchant: Great, off you go!");
            System.out.println("King: Good luck out there, " + name);
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("");
            System.out.println("");
            System.out.println("--*Final Battle Arc*--");
            System.out.println("");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("I am back at where it started.");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: *HEAVY BREATHING**HEAVY BREATHING**HEAVY BREATHING*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Oyy, wake up lazy head, I'M BAAAAAAAACK!!!");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: *ROAAAAAAAAAAAAAR*");
            System.out.println("");
            System.out.println("Press x to land the first blow!");
        }
        hits.remove("3");
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: *AHHHHHH*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            lhits = hits.size();
            System.out.println("Looks like the dragon took some damage, and my charge count is now " + lhits);
            System.out.println("");
            System.out.println("Press x to land the second blow!");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: *AHHHHHH*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        hits.remove("2");
        if (play.charAt(0) == 'x' )
        {
            lhits = hits.size();
            System.out.println(lhits + " more hit and I can finish the dragon!!!");
            System.out.println("");
            System.out.println("Press x to land the third blow!");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: *AHHHHHH*");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        hits.remove("1");
        if (play.charAt(0) == 'x' )
        {
            lhits = hits.size();
            System.out.println("NOWWWWWWWWWWWWWWWWWWWW!");
            System.out.println("");
            System.out.println("Press x to land the final blow!");
        }
        if (play.charAt(0) == 'x' )
        {
            if(elemental.charAt(0) == 'f')
            {
                System.out.println("Fire element, use PHENOIX");
            }
            else if (elemental.charAt(0) == 'w')
            {
                System.out.println("Water element, use TSUNAMI");
            }
            else if (elemental.charAt(0) == 'a')
            {
                System.out.println("Air element, use TORNADO");
            }
            else
            {
                System.out.println("Earth element, use EARTHQUAKE");
            }
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Dragon: AHHHHHHHHHHHHHHHHHHHHHHHhhhh-h-h..h..h...");
            System.out.println("");
            System.out.println("Press x to continue");
        }
        if (play.charAt(0) == 'x' )
        {
            System.out.println("Man, that training paid off, time to return home and celebrate with Jim and everyone else!");
            System.out.println("");
            System.out.println("Press x to end game");
        }
    }
}

Advertisement

Answer

It looks like you stopped following the pattern that you applied in the beginning. As you’ll see, prior to line 310, you have used

play = scan.nextLine();
if (play.charAt(0) == 'x' )

Where you call the scanner to read a new line before checking the character. After line 310, you have stopped using the play = scan.nextLine(); which means the program won’t halt to wait for user input and will continue because player.charAt(0) will always equal x (or whatever the previous input was).

Find all the instances where you are checking for the character input and make sure you have called the nextLine()

Edit:

Just a word of advice as someone who’s had to mark first-year university projects.

  • Consider storing your dialogue in something like an array (especially in this case where the dialogue is very linear) and then using a while loop to iterate over it. Look up line separators for Java.
  • Consider what happens when the user doesn’t press “x” and how you would handle that correctly.
  • Add some developer comments on why you did something a certain way.
  • Look up the concept of DRY (don’t repeat yourself) and look at ways to improve your code in this fashion.
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement