EDIT: SOLVED
I am having problems with getting my Scanner to work properly. I have googled a lot about this and they suggest putting a hasNextInt() but this just skips the nextLine and so it keeps using the default value of choice which spams the output. If I take the hasNextInt out, it gives a No Such Element Exception. The problem is in the fight method. The source code is below.
public static void game()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
StarterSword starterw = new StarterSword();
weapons[0] = starterw;
StarterArmour startera = new StarterArmour();
armour[0] = startera;
Heal heal = new Heal();
items[0] = heal;
System.out.println();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Lets start with an easy monster fight first.\nLater on it will be harder as monsters evolve.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int[] b = fight(a, starterw, startera, heal);
a++;
int coin = b[0];
coins = coins + coin;
System.out.println("You now have " + coins + " coins");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
contGame();
}
public static void contGame(){
System.out.println("Now a slightly harder boss!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner choose = new Scanner(System.in);
while(a <= 40)
{
Weapon weapon = PVMGame.weapons[0];
Armour armour = PVMGame.armour[0];
Items heal = PVMGame.items[0];
int[] b = fight(a, weapon, armour, heal);
int coin = b[0];
coins = coins + coin;
System.out.println("You now have " + coins + " coins");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(b[1] == 1)
{
System.out.println("You won! You can go to the next level!");
a++;
}
else
{
System.out.println("You lost!");
}
choose.close();
}
}
public static int[] fight(int level, Weapon weapon, Armour armour, Items heal){
int[] a = new int[2];
int b = 0;
int c = 0;
int damage;
int health ;
health = level * 30;
damage = (level * 4) - 1;
System.out.println("\nThis Monster has " + health + " health and " + damage + " damage.\n1)Attack\n2)Use Heal(You can only have one of each type of item)\n3)QUIT THIS WHOLE PROGRAMME!!");
boolean isAlive = true;
boolean isPAlive = true;
boolean win = false;
int pHealth = armour.hp;
int attack = weapon.damage;
int healing = heal.healHP;
String weaponT = weapon.name;
String armourT = armour.name;
String healT = heal.name;
int max = pHealth;
int totalD = 0;
int choice = 1;
@SuppressWarnings("resource")
Scanner userInput = new Scanner(System.in);
while(isAlive && isPAlive){
if(userInput.hasNextInt()){
choice = userInput.nextInt(); //This is where it goes wrong
}
if(choice == 1)
{
health = health - attack;
System.out.println("\nYou inflicted " + attack + " damage on the enemy with your" + weaponT + "\nHe now only has " + health + " health left.");
if(health <= 0){
b = (level * 40) - 5;
System.out.println("You defeated the monster! You won " + b + " coins.");
a[0] = b;
win = true;
isAlive = false;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pHealth = pHealth - damage;
System.out.println("\nThe Monster inflicted " + damage + " damage on your" + armourT + "\nYou only have " + pHealth + " health left.");
totalD = totalD + damage;
if(pHealth <= 0){
System.out.println("You failed.");
isPAlive = false;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("\n1)Attack\n2)Use Heal\n3)QUIT THIS WHOLE PROGRAMME!!");
}
else if(choice == 2)
{
pHealth = pHealth + healing;
if(pHealth > max){
healing = totalD;
pHealth = max;
}
if(healing != 0){
System.out.println("\nYou healed " + healing + " health on yourself with your" + healT + "\nYou have " + pHealth + " health left.");
}
else{
System.out.println("You have no healing potions left!");
}
healing = 0;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pHealth = pHealth - damage;
System.out.println("\nThe Monster inflicted " + damage + " damage on your" + armourT + "\nYou only have " + pHealth + " health left.");
totalD = totalD + damage;
if(pHealth <= 0){
System.out.println("You failed.");
isPAlive = false;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("\n1)Attack\n2)Use Heal\n3)QUIT THIS WHOLE PROGRAMME!!");
}
else if(choice == 3)
{
System.exit(0);
}
else
{
System.out.println("TYPE EITHER 1, 2, OR 3");
fight(level,weapon,armour,heal);
}
}
if(win){
c = 1;
}
if(win == false){
c = 0;
}
a[1] = c;
return a;
}