Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Expected true but was false...

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Expected true but was false...

    Hey there,


    I am really stuck with one of my codes. It is about the paper stone scissors game.

    Here is my code:

    public String rockPaperScissors(String choice) {
    		String userChoice = "";
    		String computerChoice = "";
    		String output ="";
     
    		if (userChoice.equalsIgnoreCase("rock")) {
    			if (computerChoice.equalsIgnoreCase("scissors"))
    				output += "USER:rock vs COMP:scissors user wins";
    			else if (computerChoice.equalsIgnoreCase("paper"))
    				output += "USER:rock vs COMP:paper COMPUTER wins";
    			else
    				output += "USER:paper vs COMP:paper it is a draw";
    		} else if (userChoice.equalsIgnoreCase("paper")) {
    			if (computerChoice.equalsIgnoreCase("scissors"))
    				output += "USER:paper vs COMP:scissors COMPUTER wins";
    			else if (computerChoice.equalsIgnoreCase("rock"))
    				output += "USER:paper vs COMP:rock user wins";
    			else
    				output += "USER:scissors vs COMP:scissors it is a draw";
    		} else if (userChoice.equalsIgnoreCase("scissors")) {
    			if (computerChoice.equalsIgnoreCase("rock"))
    				output += "USER:scissors vs COMP:rock COMPUTER wins";
    			else if (computerChoice.equalsIgnoreCase("paper"))
    				output += "USER:scissors vs COMP:paper user wins";
    			else
    				output += "USER:rock vs COMP:rock it is a draw";
    		}
    		return output;
    	}

    I always get the error: Expected true but was false

    Can anybody help me fix my code?

    Thank you

    ps.
    I am a beginner so be kind please
    Last edited by Taronus; January 2nd, 2020 at 06:56 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

    I always get the error: Expected true but was false
    What statement gives that error? I do not see that message in the code.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Taronus (January 2nd, 2020)

  4. #3
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Expected true but was false...

    This is my Test code:


    @Test 
    	void testRockPaperScissors() {
    		String[] answers = {"USER:rock vs COMP:scissors user wins",
    				"USER:paper vs COMP:rock user wins",
    				"USER:scissors vs COMP:paper user wins",
    				"USER:scissors vs COMP:rock COMPUTER wins",
    				"USER:rock vs COMP:paper COMPUTER wins",
    				"USER:paper vs COMP:scissors COMPUTER wins",
    				"USER:scissors vs COMP:scissors it is a draw",
    				"USER:paper vs COMP:paper it is a draw",
    				"USER:rock vs COMP:rock it is a draw"};
    		assertTrue(Arrays.asList(answers).contains(wordPlay.rockPaperScissors("rock")));
    		assertTrue(Arrays.asList(answers).contains(wordPlay.rockPaperScissors("paper")));
    		assertTrue(Arrays.asList(answers).contains(wordPlay.rockPaperScissors("scissors")));
    	}

    And the Error:

    org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
    at org.junit.jupiter.api.AssertionUtils.fail(Assertio nUtils.java:55)
    at org.junit.jupiter.api.AssertTrue.assertTrue(Assert True.java:40)
    at org.junit.jupiter.api.AssertTrue.assertTrue(Assert True.java:35)
    at org.junit.jupiter.api.Assertions.assertTrue(Assert ions.java:162)
    at WordPlayUnitTests4.testRockPaperScissors(WordPlayU nitTests4.java:57)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invo ke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl. invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.platform.commons.util.ReflectionUtils.in vokeMethod(ReflectionUtils.java:675)
    at org.junit.jupiter.engine.execution.MethodInvocatio n.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInter ceptorChain$ValidatingInvocation.proceed(Invocatio nInterceptorChain.java:125)
    at org.junit.jupiter.engine.extension.TimeoutExtensio n.intercept(TimeoutExtension.java:132)
    at org.junit.jupiter.engine.extension.TimeoutExtensio n.interceptTestableMethod(TimeoutExtension.java:12 4)
    at org.junit.jupiter.engine.extension.TimeoutExtensio n.interceptTestMethod(TimeoutExtension.java:74)
    at org.junit.jupiter.engine.execution.ExecutableInvok er$ReflectiveInterceptorCall.lambda$ofVoidMethod$0 (ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvok er.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInter ceptorChain$InterceptedInvocation.proceed(Invocati onInterceptorChain.java:104)
    at org.junit.jupiter.engine.execution.InvocationInter ceptorChain.proceed(InvocationInterceptorChain.jav a:62)
    at org.junit.jupiter.engine.execution.InvocationInter ceptorChain.chainAndInvoke(InvocationInterceptorCh ain.java:43)
    at org.junit.jupiter.engine.execution.InvocationInter ceptorChain.invoke(InvocationInterceptorChain.java :35)
    at org.junit.jupiter.engine.execution.ExecutableInvok er.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvok er.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTest Descriptor.lambda$invokeTestMethod$6(TestMethodTes tDescriptor.java:202)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.jupiter.engine.descriptor.TestMethodTest Descriptor.invokeTestMethod(TestMethodTestDescript or.java:198)
    at org.junit.jupiter.engine.descriptor.TestMethodTest Descriptor.execute(TestMethodTestDescriptor.java:1 35)
    at org.junit.jupiter.engine.descriptor.TestMethodTest Descriptor.execute(TestMethodTestDescriptor.java:6 9)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$5(NodeTestTask .java:135)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$7(NodeTestTask .java:125)
    at org.junit.platform.engine.support.hierarchical.Nod e.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$8(NodeTestTask .java:123)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.executeRecursively(NodeTestTask.java:122 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    at org.junit.platform.engine.support.hierarchical.Sam eThreadHierarchicalTestExecutorService.invokeAll(S ameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$5(NodeTestTask .java:139)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$7(NodeTestTask .java:125)
    at org.junit.platform.engine.support.hierarchical.Nod e.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$8(NodeTestTask .java:123)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.executeRecursively(NodeTestTask.java:122 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    at org.junit.platform.engine.support.hierarchical.Sam eThreadHierarchicalTestExecutorService.invokeAll(S ameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$5(NodeTestTask .java:139)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$7(NodeTestTask .java:125)
    at org.junit.platform.engine.support.hierarchical.Nod e.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.lambda$executeRecursively$8(NodeTestTask .java:123)
    at org.junit.platform.engine.support.hierarchical.Thr owableCollector.execute(ThrowableCollector.java:73 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.executeRecursively(NodeTestTask.java:122 )
    at org.junit.platform.engine.support.hierarchical.Nod eTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.Sam eThreadHierarchicalTestExecutorService.submit(Same ThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.Hie rarchicalTestExecutor.execute(HierarchicalTestExec utor.java:57)
    at org.junit.platform.engine.support.hierarchical.Hie rarchicalTestEngine.execute(HierarchicalTestEngine .java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.e xecute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.l ambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.w ithInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.e xecute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.e xecute(DefaultLauncher.java:137)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestR eference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:210)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

    org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
    at org.junit.jupiter.api.AssertionUtils.fail(Assertio nUtils.java:55)
    at org.junit.jupiter.api.AssertTrue.assertTrue(Assert True.java:40)
    at org.junit.jupiter.api.AssertTrue.assertTrue(Assert True.java:35)
    at org.junit.jupiter.api.Assertions.assertTrue(Assert ions.java:162)
    at WordPlayUnitTests4.testRockPaperScissors(WordPlayU nitTests4.java:57)
    What statement is at line 57? What test was made on that line?

    What value is in the String returned for the method: wordPlay.rockPaperScissors
    Add print statements so you can see what each of those calls to the method returns.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Expected true but was false...

    else if (computerChoice.equalsIgnoreCase("paper"))

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

    I was asking about line 57 in WordPlayUnitTests4.java

    What value is in the String returned for the method: wordPlay.rockPaperScissors
    Add print statements so you can see what each of those calls to the method returns.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Expected true but was false...

    so sorry !

    assertTrue(Arrays.asList(answers).contains(wordPla y.rockPaperScissors("rock")));


    Now that you say that I notice that I do not use the String "choice" anywhere...

    Thought the answers are stored in it but they're obviously not... So I ain't got a clue what is stored in it... It should generally be the answers from the test unit I think.

    I got a print statement... however, it does not print anything for this task.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

      assertTrue(Arrays.asList(answers).contains(wordPlay.rockPaperScissors("rock")));
    What is that statement supposed to test? Can you explain that?

    Print out the value returned by: wordPlay.rockPaperScissors("rock") so you can see what is being tested.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Expected true but was false...

    It tests who has won by the selection of rock

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

    The array answers contains ALL the responses. How does that one statement check for the winner of "rock"? vs the winner for "paper"?

    Did you add a print statement to print what was returned by: wordPlay.rockPaperScissors("rock")?
    What was printed?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Jan 2020
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Expected true but was false...

    Yes, but it does not print anything. It loads and then just stops
    I have done it for all

    System.out.println(wordPlay.rockPaperScissors("roc k"));
    System.out.println(wordPlay.rockPaperScissors("pap er"));
    System.out.println(wordPlay.rockPaperScissors("sci ssors"));

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Expected true but was false...

    it does not print anything
    Ok, look at the code to see what is supposed to be in the variable that is returned by the method.
    How would it be possible for the method to leave the value in the variable as an empty String? That does not print anything.
    Add some print statements in the code that print out the values of all the variables used in the if statements that are used to control what is supposed to be returned.

    One logic problem I see in the code is a missing else statement at the end of the if/else if chain. There needs to be an else statement at the end that prints a message if none of the above statements were true. Add the following at the end of the if/else if chain:
       else {
          System.out.println("No match for choice="+choice);
       }
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. True and False that
    By kindon in forum Totally Off Topic
    Replies: 0
    Last Post: December 6th, 2013, 06:33 PM
  2. True False Bitmap Program
    By BiggerBore in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 7th, 2013, 06:49 PM
  3. Objects, classes, lists true and false HELP!
    By klskl in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2013, 07:05 AM
  4. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM

Tags for this Thread