-3
    System.out.println("\n");
    System.out.println("What is the upgrade of your Hammer?  You must choose a number.");
    System.out.println("1. No Upgrade");
    System.out.println("2. Sapphire.");
    System.out.println("3. Emerald.");
    System.out.println("4. Ruby.");
    System.out.println("5. Diamond."); 
    String var1 = Scanner.nextLine(); 




    //char hammerlevel = (char) System.in.read();
    System.out.println();
    System.out.println("\n");

    double noviceStardustPotion = 6023.33333333; //amount of experience per potion average
    if (var1.equals('No Upgrade')
        {

        }

It was working originally when I used the numbers, but I don't want my users typing a number, I want them to type the actual word.

 if (hammerlevel == '1')
 {

 }

I've researched on the .equals method and I cannot find any examples like mine that use the method with a string in the parentheses

Any ideas or even blatant answers that could help?

Trechubet
  • 15
  • 2
  • equals is used to compare objects by its content.... meanwhile == is used for references – ΦXocę 웃 Пepeúpa ツ Mar 06 '17 at 08:28
  • 2
    var1.equals("No Upgrade") – rathna Mar 06 '17 at 08:29
  • I wanted to compare the System.in.read prompt with what it said, and if it said something the if statement would operate. Could I do it with if (hammerlevel == 'No Upgrade')? – Trechubet Mar 06 '17 at 08:30
  • if (var1.equals('No Upgrade')) { } - returns true or flase – kzharas210 Mar 06 '17 at 08:30
  • Ok, so I can only get a true or false answer. How would I make it rely on the words being typed? – Trechubet Mar 06 '17 at 08:32
  • You are using it right, `if (var1.equals("No Upgrade")`, you are just missing closing brackets `if (var1.equals("No Upgrade"))`. You should also use `"`, not `'`. – Guy Mar 06 '17 at 08:32
  • It should be noted that you should probably upper or lower both strings before checking with the equals method – Sdyess Mar 06 '17 at 08:33
  • In Java, a literal in single quotes is a `char`, and thus can only have one character, e.g. `'a'`; only a literal in double quotes is a `String`, e.g. `"abc"`. – Andy Turner Mar 06 '17 at 08:42
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – rakwaht Mar 06 '17 at 08:42
  • @rakwaht no, it's not about that. It's a simple question of the syntax to write a string literal. OP is already trying to use `equals`. – Andy Turner Mar 06 '17 at 08:43

3 Answers3

2

First, you're if statement is a bit wonky. It's missing a closing a parenthesis and it should be using " " instead of ' ' to denote a string literal.

if (var1.equals("No Upgrade"))
{

}

And a bit about how equals() works.

String input = "No Upgrade";
String input2 = "no upgrade";
String input3 = "no upgrade";

//this returns false because the strings are not the same value
if (input.equals(input2) {
    //do action
}

//this returns true because the string values are the same
if (input.equals("No Upgrade") {
    //do action
}

//this returns true because the string values are the same
if (input2.equals(input3) {
    //do action
}

The equals method from string is comparing the values of the string. If it has an extra whitespace, an extra capital letter, anything, it won't be true. You can avoid this by using toUpperCase() or toLowerCase() on both strings and then checking their value, or by just using equalsIgnoreCase().

if (input1.equalsIgnoreCase("No Upgrade")) {
}
Sdyess
  • 340
  • 1
  • 8
  • 1
    [`input1.equalsIgnoreCase(input2)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#equalsIgnoreCase-java.lang.String-). – Andy Turner Mar 06 '17 at 08:48
0

Cutting down your code to just the relevant bits:

String var1 = Scanner.nextLine(); 
if (var1.equals('No Upgrade')
{

}

(Everything else is a System.out.println call, or an unused variable)

The only problem with this code is that 'No Upgrade' is not valid Java, and so does not compile: you use single quotes to denote a char literal, and thus you can only have a single character between the quotes, e.g. 'a'.

You use double quotes to specify a String literal:

String var1 = Scanner.nextLine(); 
if (var1.equals("No Upgrade")
{

}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
-1

First, check the input of the user, so the input shouldn´t be something like "you s*ck"

int var1 = Scanner.nextLine().parseInt(); 
if(var1 == 1){} 

if(var1 == 2) //You can also use int, but you don´t want, ok

if (var1.equals("1")) //Because var1 should be a number{}
kayess
  • 3,384
  • 9
  • 28
  • 45
Reisbrot
  • 37
  • 9