1

I'm a freshman computer science major in college and I am completely new to all of this stuff. We are currently using java and are on our first homework where we have to make a combat calculator for a text based game. My problem is in the if else statements that I have, where the print line statements won't print out when I run the program.

    userInput = input.next();

    int action1 = 1;
    action1 = input.nextInt();
    int action2 = 2;
    action2 = input.nextInt();
    int action3 = 3;
    action3 = input.nextInt();
    int action4 = 4;
    action4 = input.nextInt();

    if (userInput == action1){
        System.out.println("You strike the goblin with your sword for 12 damage.");
    }
    else if (userInput.equals(action2)){
        System.out.println("You cast the weaken spell on the goblin.");
    }
    else if (userInput.equals(action3)){
        System.out.println("You focus and charge your magic power.");
    }
    else if (userInput.equals(action4)){
        System.out.println("You run away!");
    }

}

I also don't know how to put codes properly in this website so sorry if it's a little hard to understand. But anyway, what am I doing wrong to where my output for the if...else statements won't print?

kplandry
  • 13
  • 1
  • 3
  • 1
    why do'nt you put the entire code? – innoSPG Sep 10 '15 at 17:49
  • What type does your variable `userInput` have? – Seelenvirtuose Sep 10 '15 at 17:49
  • I did initially, but this is my first time using this site and the whole code wasn't correctly formatting for me. – kplandry Sep 10 '15 at 17:50
  • 1
    Why do you set `action1 = 1;` and read it again? Same question for `action2` to `action4`? – innoSPG Sep 10 '15 at 17:51
  • I have it set as String userInput; – kplandry Sep 10 '15 at 17:52
  • Why are you using .equals for something that looks like an integer comparison? Just change it to be userInput == action. Presuming of course userInput is indeed an integer. Also I think you are reading the input too many times, you probably only need one variable named action = input.nextInt(). Don't change the values of the other actions. – Sh4d0wsPlyr Sep 10 '15 at 17:52
  • @kplandry put the full code, someone will help formating and you will learn gradually – innoSPG Sep 10 '15 at 17:52
  • like i said i'm completely new to this so i don't really know what i'm doing – kplandry Sep 10 '15 at 17:53
  • Two mistakes. You are using == for comparison & directly comparing String with int – Ravindra babu Sep 10 '15 at 17:53
  • @kplandry what I am saying is that you copy your entire file content and add it in your question, someone will help you with the formatting. You can read this http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks for code formatting. – innoSPG Sep 10 '15 at 18:15

2 Answers2

1

Well, first of all, I'm assuming you are using a Scanner to read user's input. If so, input.next() returns a String and hence if (userInput == action1){ is not valid.

Moreover, (assuming again) if you correct that, the comparison of those types, even if they have the same value they won't match.

Your issue lies in the difference between equals method and == operator.

Try this:

int userInput = input.nextInt();

int action1 = input.nextInt();
int action2 = input.nextInt();
int action3 = input.nextInt();
int action4 = input.nextInt();

if (userInput == action1) {
  System.out.println("You strike the goblin with your sword for 12 damage.");
} else if (userInput == action2) {
  System.out.println("You cast the weaken spell on the goblin.");
} else if (userInput == action3) {
  System.out.println("You focus and charge your magic power.");
} else if (userInput == action4) {
  System.out.println("You run away!");
}

Notice I changed a little bit the bindings/assignments and the type for userInput is now int.

x80486
  • 6,627
  • 5
  • 52
  • 111
  • I changed everything as you have it here and it still won't print. 'String option1 = " 1.) Sword Attack"; System.out.println(option1); String option2 = " 2.) Cast Spell"; System.out.println(option2); String option3 = " 3.) Charge Mana"; System.out.println(option3); String option4 = " 4.) Run Away"; System.out.println(option4);' – kplandry Sep 10 '15 at 18:05
  • i still don't know how to format code on this site, sorry. but what i put in the last comment is what i have as the options for the player to pick when the program is run. i'm still confused as to how to set a number to print out the actions – kplandry Sep 10 '15 at 18:07
  • @kplandry your problem is not well stated, so answers are guess, I just added one below that must possibly work well for you. – innoSPG Sep 10 '15 at 18:07
  • @kplandry You didn't post the written options for the actual "options" in the original source code; nobody can figure it out that for `action1` you were asking `1.) Sword Attack` – x80486 Sep 10 '15 at 19:09
-1

I guess you need something like the example below. I do not see why you set action1 = 1 and read it again using action1 = input.nextInt();. The same observation for action2, action3, action4. I also guess you need nextInt() for userInput because you are comparing it to int. You should also think of the case where the user did not input a right choice; I added an else at the end.

full example:

import java.util.Scanner;

public class Test {

    public static void main(String... args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter your choice:"); // if reading from standard input (waiting from the keyboard), this is useful.
        int userInput = input.nextInt();

        int action1 = 1;
        //action1 = input.nextInt(); // I commented this out because I do not think it is useful
        int action2 = 2;
        //action2 = input.nextInt(); // I commented this out because I do not think it is useful
        int action3 = 3;
        //action3 = input.nextInt(); // I commented this out because I do not think it is useful
        int action4 = 4;
        //action4 = input.nextInt(); // I commented this out because I do not think it is useful

        if (userInput == action1) {
            System.out.println("You strike the goblin with your sword for 12 damage.");
        } else if (userInput == action2) {
            System.out.println("You cast the weaken spell on the goblin.");
        } else if (userInput == action3) {
            System.out.println("You focus and charge your magic power.");
        } else if (userInput == action4) {
            System.out.println("You run away!");
        } else{
            System.out.println("Wrong choice!");
        }
    }

}

Edited: Adding example of output.

Compiling and running:

$javac Test.java
==================== run 1
$java Test
Enter your choice:
1
You strike the goblin with your sword for 12 damage.

==================== run 2
$java Test
Enter your choice:
2
You cast the weaken spell on the goblin.

==================== run 3
$java Test
Enter your choice:
3
You focus and charge your magic power.

==================== run 4
$java Test
Enter your choice:
4
You run away!

==================== run 5
$java Test
Enter your choice:
5
Wrong choice!
innoSPG
  • 4,588
  • 1
  • 29
  • 42
  • When I run the program, the first line (Enter your choice:) is printed, but then when I type in a number, the line to be printed with it (You strike the goblin..., You cast the weaken spell..., etc.) doesn't print. Hope that makes my problem a little clearer – kplandry Sep 10 '15 at 18:16
  • @kplandry Just to make sure: you have to hit the key after entering your number. This example that I provide should then print one of your message or the last message "Wrong choice" that I added. – innoSPG Sep 10 '15 at 18:18
  • whenever i enter one of the first 4 actions, there is no output, but when i enter a number other than 1, 2, 3, or 4, the "Wrong choice!" message prints. could that lead to something? – kplandry Sep 10 '15 at 18:32
  • That is weird, as you can see the edit of the answer, I actually ran the code and get the expected result for each choice. Can you copy the full example here and try it? I mean keeping your original work aside. – innoSPG Sep 10 '15 at 18:36
  • I found out my problem. I don't really know how to explain it but this example that you posted really helped me and the output is now printing with all of the actions. thank you so much! – kplandry Sep 10 '15 at 18:42
  • @kplandry, great that you solve your problem. If you don't mind, accept the answer as it helped you to solve your problem. – innoSPG Sep 10 '15 at 18:45
  • Downvoter, It is always good to downvote to help clear S.O from junk, help people easily identify good questions/answer. But when you downvote an accepted answer, take the time to explain your action. You should actually take the time to say a word about each downvote. – innoSPG Sep 11 '15 at 13:19