user3054759
user3054759

Reputation: 3

Java, returning Strings error

I am very new to programming and am quite young.

I have no friends/family who can help me so I am seeking help on the internet. There is problem with my code as it isn't working as I intend it.

Instead of printing out what the variable "TheChoice", it just ends.

This isn't all the code and I have consised it so that it will be easier to read and maybe more people will be able to quickly answer.

However, this is definately the part of my code which I have messed up).

public String Choices(String value1, String value2)
{
  Scanner x = new Scanner(System.in);
  if(x.next() == value1){return value1;}
  if(x.next() == value2){return value2;}
}


// Separate class...

ChoiceClass Object1 = new ChoiceClass();
String TheChoice = Object1.Choices("Ham", "Cheese");
System.out.println(TheChoice);

Upvotes: 0

Views: 61

Answers (3)

Mena
Mena

Reputation: 48404

There's a number of issues with your code.

  • Firstly, you compare Strings with == instead of equals (there's a ton of literature about String comparison and Object equality in Java, I suggest you take a look here and here.
  • Secondly, you don't always return a value in your choices method. Your method must return a String (even in its default value, as null), but you're not considering user inputs other than given arguments.
  • Also your Scanner next wouldn't work as you're calling it twice, when you only want to call it once.
  • Finally, you should take a look at Java naming conventions: method names are camelBack. See here for code conventions.

Here's a snippet that'll probably help you out:

public static String choices(String value1, String value2) {
    Scanner x = new Scanner(System.in);
    System.out.println("Type your choice and ENTER...");
    String input = x.nextLine();
    if (input.equals(value1)) {
        return value1;
    }
    else if (input.equals(value2)) {
        return value2;
    }
    // handling other user inputs
    else {
        return "nothing";
    }
}

public static void main(String[] args) {
    // usage of method. Note that you could also refactor with varargs, as in: 
    // String... values in method signature.
    // This way you could iterate over values and check an arbitrary number of values, 
    // instead of only 2.
    System.out.println(choices("foo", "bar"));
}

Output:

Type your choice and ENTER...

... then either "foo" or "bar" or "nothing" according to input.

Upvotes: 2

Kishore
Kishore

Reputation: 300

You can use something similar to this:-

public String Choices(String value1, String value2)
    {
        Scanner x = new Scanner(System.in);
        String option=x.next();
        if(option!=null)
        {
            if(option.equals(value1)) return value1;
            else if (option.equals(value2)) return value2;

        }
        else return option;

    }

Upvotes: 1

Encyclopedia
Encyclopedia

Reputation: 144

If you want to compare Strings just use equals no need to use Scanner here.

Upvotes: 0

Related Questions