Hi, its has been a long time since I have used java and I would like some help making this loop as long as you say yes when you say no the loop will stop but not the program

if (place.equals("swordsmith"))
            {
                System.out.println("Hello " + name +"! Would you like to upgrage your sword for " + UPGRADECOST + " gold?");
                smith = input.next();
                smith = smith.toLowerCase();


                if(smith.equals("yes"))
                {
                    if (playerGold>=UPGRADECOST)
                    {
                        playerAttack+=UPGRADE;
                        playerGold-=UPGRADECOST;
                    }
                    else
                    {
                        System.out.println("Please come back when you have more gold!");
                    }
		 }
            }

Also can someone tell me how I could store and load character files on my java application

making this loop as long as you say yes

Where is the code for the loop?
All I see are some if statements?

I have a loop at the beginning of the program

while(playerHP>0)

but what I want is to have the code I originally posted to loop until you say no
when you say yes right now it upgrades the attack but you have to type in
swordsmith then yes over and over again. I want it so that it stays in the swordsmith shop until you say no

this is a text based rpg

I want it so that it stays in the swordsmith shop until you say no

Write a do while loop that continues until the user says says to exit the loop.

Member Avatar for Mouche

Here's an example of how to do it. Take a look at the do...while loop and then try adding one in your own code. The key to the loop is to make your condition the "no" from the user input.

import java.util.Scanner;

public class swordsmith {
    public static void main(String[] args) {
        int gold = 20;
        int cost = 5;
        int swords = 0;
        String smith;
        Scanner input = new Scanner(System.in);

        // Loop through the swordsmith menu until the user
        // says they don't way to upgrade their sword
        do {
            System.out.println("Hello. Would you like to upgrade your sword for " + cost + " gold?");
            System.out.println("You have " + gold + " gold.");
            smith = input.next();
            smith = smith.toLowerCase();
            if (smith.equals("yes")) {
                // Purchase a sword upgrade if you have enough money
                if (gold >= cost) {
                    gold -= cost;
                    swords++;
                    System.out.println("You now have " + swords + " sword upgrades.");
                } else {
                    System.out.println("Please come back when you have more gold!");
                }
            }
        } while (! smith.equals("no"));
        System.out.println("Bye!");
    }
}
commented: Thank you so much! You were very helpful :) +0

Thanks you so much LaMouche! I didn't learn do while statements in my java class it was only one semester (I took it 2 years ago). I am trying to make the text based rpg I made for my final project better and more user friendly. I plan on making quests and more features.

Now to the second part of my question, is there a way to store and load character data if it is just a regular java application? Like a character file or something.

is there a way to store character data

Do you want to write it to a disk file? There are java classes that will write Strings to a disk file.

Sorry I do not know what a disk file is. I only took java one semester because that is what my school had.

I want it to be able to save a characters file and load (password to load if not to hard) it up so that you don't have to start all over when you close the application. What ever the simplest way to do it is.

Where do you want to save the data?

In a folder of saved characters files or a type of file that just has all the character data on it (what ever is easiest). I don't know how I would save the data at all.

Would it help if I pmed (don't wana post the whole thing here) you my current java program?

Look at the PrintWriter class.

Can you please give me an good example the ones I look through online are usually not what I am looking for. Can you also, explain the example too. I learn best by applicable examples.

Post some of the examples you found online and explain what you need changed about them.

I don't know anything about print write so and I don't know what would change them to make them work for me.

I will read the tutorials and then once I understand it more Ill post some examples in a few days

Sorry i havent posted code i have been very busy lately with school and stuff
I will look at stuff sunday and post then hopefully

i have been to busy and this isnt a priority this thread can be closed now

ill ask again later when i have more time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.