Issue
EDIT again:
each time I run this and input the "String a", all methods get called and I don't know why.
I know its all newbie stuff but counter intuitively, there are too many tutorials and threads on java to properly troubleshoot basic issues like this.
import java.util.*; public class ShopTest {
public static Scanner navigate = new Scanner(System.in);
public static Scanner playerStats = new Scanner(System.in);
public static Scanner shop = new Scanner(System.in);
static int playerGold = 0;
static String items;
static int ironSword = 200;
static int rustySword = 75;
static int lightLeatherArmor = 50;
static int minorHealthpotion = 25;
static String bought = "item added to inventory";
static String notBought = "Sorry, you don't have enough for that!";
public static void main(String[] args) {
System.out.println("Welcome player.\nWhat is your name?");
String playerName = playerStats.next();
System.out.println("\nAh!" + playerName + "\n\nWe've been expecting you.\nAs we agreed, you get half the gold now and half after you kill that goblin!\nYou recieved 150 gold.");
playerGold = playerGold +150;
Navigate();
}
public static void Shop() {
System.out.println("\nWelcome to my shop\nWould you like to see my wares?");
String shopChoice1 = shop.next();
if (shopChoice1.equals("yes")) {
System.out.println("\nSee anything you need?\nIron sword: " + ironSword + "\nRusty sword: " + rustySword + "\nLight leather armor: " + lightLeatherArmor + "\nMinor health potion: " + minorHealthpotion);
}
String shopChoice2 = shop.next();{
System.out.println("ok");
if (shopChoice2.equals("iron sword")) {
IronSword();}
else if (shopChoice2.equals("rusty sword")) {
RustySword();}
else if (shopChoice2.equals("light leather armor")) {
LightleatherArmor();}
else if (shopChoice2.equals("minor health potion")) {
MinorhealthPotion();}
else if (shopChoice2.isEmpty()) {
Shop();}
}
}
public static void IronSword() {
if (playerGold >= ironSword)
System.out.println(bought);
items = items + "Iron sword,\n";
if (playerGold < ironSword)
System.out.println(notBought);
Shop();
}
public static void LightleatherArmor() {
}
public static void RustySword() {
if (playerGold >= rustySword)
System.out.println(bought);
items = items + "Rusty Sword,\n";
if (playerGold < rustySword)
System.out.println(notBought);
Shop();
}
public static void MinorhealthPotion() {
}
public static void Inn() {
System.out.println("**You enter the inn and approach the barkeep**\nHow can I help you?");
}
public static void Inventory() {
System.out.println("Gold: " + playerGold + "\nItems: \n" + items + "\n\n");
Navigate();
}
public static void Navigate() {
System.out.println("\nWhat next?\n Shop\n Inn\n Inv");
String a = navigate.nextLine();
if (a.equals("shop"))
Shop();
else if (a.equals("inn"))
Inn();
else if (a.equals("inv"))
Inventory();
}
}
*need add more text for some reason!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*need add more text for some reason!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*need add more text for some
Solution
The default delimiter for the Scanner is whitespace. Which means it will break up the string on every whitespace (space, tabs, newlines). So if you enter "iron sword" you will only get "iron" on shop.next() and "sword" on the call after.
Since you want to read in whole lines, you can set the Delimiter to a newline. Like this:
public static Scanner shop = new Scanner(System.in).useDelimiter("\\n");
The Javadoc for the Scanner class mentions this: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
BTW: Which IDE do you use? If you are new to programming make yourself familiar with it's debugging features. You can set a breakpoint right after the shop.next() call and then inspect the content of shopChoice2. This would have shown you that shop.next() does not do what you expect it to do.
EDIT:
There is a mistake after you edited your code:
if (shopChoice2.equals("iron sword"))
System.out.println("ok");
IronSword();}
if (shopChoice2.equals("rusty sword"))
This should be:
if (shopChoice2.equals("iron sword")) {
System.out.println("ok");
IronSword();}
} else if (shopChoice2.equals("rusty sword"))
Only "System.out.println("ok");" is being called if the if statement is true. Note that only the next statement is called if an "if" statement is true. If you need more that one statement to be executed you need to enclose those in curly brackets. As a general rule one should ALWAYS use curly brackets with if statements. Even if there is only one statement. This helps avoiding making mistakes like that.
EDIT 2:
This should work. Find the differences to your code:
String shopChoice2 = shop.next();
System.out.println(shopChoice2);
if (shopChoice2.equals("iron sword")) {
IronSword();"
} else if (shopChoice2.equals("rusty sword")) {
RustySword();
} else if (shopChoice2.equals("light leather armor")) {
LightleatherArmor();
} else if (shopChoice2.equals("minor health potion")) {
MinorhealthPotion();
} else if (shopChoice2.isEmpty()) {
Shop();
}
Answered By - nharrer
Answer Checked By - Robin (JavaFixing Admin)