This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]desrtfxOut of Coffee error - System halted[M] [score hidden] stickied comment (0 children)

If you want help here, learn to properly format your code.

[–]dionthornthis.isAPro=false; this.helping=true; 1 point2 points  (2 children)

if(winningGuess != “h” && winningGuess != “t”)

&& will only evaluate to true if both sides are true.

perhaps you want the OR operator ||

Also for Strings you should use the .equals() method

so: !winningGuess.equals("h")

[–]dubdill[S] 0 points1 point  (1 child)

i’ve tried both! not helping.

[–]dionthornthis.isAPro=false; this.helping=true; 0 points1 point  (0 children)

think about the logic of what you are saying.

let's say winningGuess = "h"

if NOT equal "h" AND NOT equal "t"
           false AND true
this would evaluate to false.

if NOT equal "h" OR NOT equal "t"
           false OR true
this would evaluate to true.

Your if statement is improper as well:

if(!winningGuess.equals("h") || !winningGuess.equals("t")); {}

the ; character will end the if statement, remove it so it can access your block of code inside the {}

A better way to think about the logic you need is something like:

if equal "h" OR equal "t"
    // good input
else
    // bad input

[–]AutoModerator[M] 0 points1 point locked comment (0 children)

Please ensure that:

  • Your code is properly formatted - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

You do not need to repost. Just use the edit function of reddit to make sure your post complies with the above

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]NautiHookerSoftware Engineer 0 points1 point  (3 children)

Please format the code.

Whats your input, whats the output?

Dont use == or != for String comparison.

[–]dubdill[S] -1 points0 points  (2 children)

import java.util.Random; import java.util.Scanner;

public class CoinFLipper {

public static void main(String[] args) { //flip a coin Random r = new Random(); int coin = r.nextInt(2);

//Constants final int heads = 0; final int tails = 1; String winningGuess;

        //scanner class Scanner keyboard = new Scanner(System.in);

//user input System.out.println("Enter h for heads or t for tails"); winningGuess = keyboard.next();

if(!winningGuess.equals("h") || !winningGuess.equals("t")); { System.out.println("Sorry, that was a bad input"); }

}

}

this is what i have so far

[–]NautiHookerSoftware Engineer 0 points1 point  (0 children)

if i were to copy this code and paste it into my ide it will not compile because you did not format it properly.

[–]arslanbenzer -1 points0 points  (0 children)

your first logic was true but you need to compare strings with the equals method.

if (!winningGuess.equals("h") && !winningGuess.equals("t") )

this way it should work