0

I'm having trouble with some Java programming and I'm pretty confused on how the comparison operands work between inputs.

What I want is for my program to take input, analyze the input, check what it's equal to, and display a message based upon their input. How am I to compare the input, to the option1 variable I have set?

Here's one file, called loadfile.java

package subsets;

import java.io.*;
import java.util.Scanner;

public class loadfile {

        public static void main(String args[]) {

        }

        public static void choose(String[] args) {

            // Set an option
            int option1 = 1;

            // Start grabbing input
            System.out.print("Choose one: ");
            Scanner scanner = new Scanner( System.in );
            String input = scanner.nextLine();

            // Parse input into an integer (expecting int)
            int selection = Integer.parseInt(input);

            // If input == option1, let's go with this code.
            if(input.equals(option1)){

                System.out.println("You choose option 1.");

            }
        }
}

I have loadfile.choose(args); in a different file, and that file handles the rest of the files I will be implementing.

Here's the file: otherfile.java

package subsets;

public class otherfile {

        public static void main(String args[]){
            System.out.println("Please select an option (using number keys): \n");
            System.out.println(
                    "1. Option 1 \n"
                    + "2. Option 2 \n"
                    + "3. Option 3 \n"
                    + "4. option 4 \n");

            // Start analyzing their choice
            loadfile.choose(args);
        }

}
Logan Butler
  • 159
  • 1
  • 2
  • 9
  • 3
    What is your question? – CubeJockey Aug 14 '15 at 20:32
  • Why can't I compare the input to the option variable I have set? (sorry, I meant to imply this). – Logan Butler Aug 14 '15 at 20:33
  • 1
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Sid Aug 14 '15 at 20:34
  • I'm not asking to how to compare strings.. – Logan Butler Aug 14 '15 at 20:35
  • 3
    Well, seemingly you should be doing `if (selection == option1)` because right now you parse the input to an `int`, but do not do anything with it. – CubeJockey Aug 14 '15 at 20:35
  • Or, you could be declaring option1 as a String, since you know you're going to be doing a comparison with another String value later on. You also know you're not doing any calculations with option1, so the fact that it's a numeral is strictly cosmetic. I hate to say, but you seem to have tunnel-visioned your way into a corner for no real reason. – MarsAtomic Aug 15 '15 at 00:11
  • Whether you compare as a string or a number matters. `10` > `9` but `"10" < "9"` – Peter Lawrey Aug 15 '15 at 08:32

3 Answers3

1

You can either change your int option1 = 1; to String option1 = "1"; or parse the user input as integer or do scanner.nextInt() instead of nextLine there are lots of options to fix this here. Integer.parseInt(String).

brso05
  • 13,142
  • 2
  • 21
  • 40
1

equals() is only valid between objects of the same type.

int is not even an object, but Java converts it implicitly to an Integer object when it passes it as the parameter to a method that expects an Object (an Integer is an Object - all classes extend Object).

But now equals is left with comparing a String object to an Integer object. Since they are not of the same type, this will always be false.

To do the comparison properly, you have to get an integer from the string by parsing it into an int. For example, using Integer.parseInt().

And... you did exactly that, but you forgot to use the result of your operation. You have the selection variable, simply compare selection instead of input.

Of course, in this case you'll simply use selection == option1 instead of using equals, because int is primitive and doesn't have methods.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

In general the equals method does not return true when comparing objects of different types. This is the implementation of String#equals(Object):

public boolean equals(Object anObject) {
   if (this == anObject) {
       return true;
   }
   if (anObject instanceof String) {
       String anotherString = (String)anObject;
       int n = count;
       if (n == anotherString.count) {
          char v1[] = value;
          char v2[] = anotherString.value;
          int i = offset;
          int j = anotherString.offset;
          while (n-- != 0) {
              if (v1[i++] != v2[j++])
              return false;
          }
          return true;
       }
   }
   return false;
}

Note the check if (anObject instanceof String).

In your case, I don't see why not simply doing:

if (selection == option1) {

since you already got the selection variable.

M A
  • 71,713
  • 13
  • 134
  • 174