0

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);
1
  • 1
    Don't use == to compare content of Strings. Use equals(). Commented Dec 1, 2013 at 16:21

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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;

    }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.