I want to create a game called Crap. The problem that I am facing is that the main while loop does not end despite the condition being set to false (by user inputing &quot;no&quot; when asked if they want to conitnue playing). What is the problem here? Comments are inserted where I think the problem is. import java.util.Scanner;  public class crap {      public static void main(String[] args) {         System.out.println(&quot;You are about to play the game of Crap. Press Enter to continue.&quot;);                  Scanner enter = new Scanner (System.in);         String hitEnter = enter.nextLine();         Scanner input = new Scanner(System.in);         String proceed = &quot;yes&quot;;                      // This loop does not exit even if proceed == &quot;no&quot;         while (proceed != &quot;no&quot;){                          int playerPoint;             int firstDice = 1 + (int) (Math.random() * 10) % 6;             int secondDice = 1 + (int) (Math.random() * 10) % 6;             int throwSum = firstDice + secondDice;                          if (throwSum == 7 || throwSum == 11) {                 System.out.println(&quot;Congratulations! You win on your first roll! You rolled &quot;              + firstDice + &quot; and &quot; + secondDice + &quot; for a total of &quot; + throwSum);             }             else if (throwSum == 2 || throwSum == 3 || throwSum == 12) {                 System.out.println(&quot;Sorry, you crapped out, you lose! You rolled &quot; + firstDice +                 &quot; and &quot; + secondDice + &quot; for a total of &quot; + throwSum);             } else {                 playerPoint = throwSum;                 System.out.println(&quot;You rolled &quot; + firstDice + &quot; + &quot; + secondDice + &quot; which is &quot;                 + playerPoint);                 System.out.println(&quot;Your point is &quot; + playerPoint + &quot; now. Good luck.&quot;);                                  while (throwSum != 7 ) {                     firstDice = 1 + (int) (Math.random() * 10) % 6;                     secondDice = 1 + (int) (Math.random() * 10) % 6;                     throwSum = firstDice + secondDice;                                          if (throwSum != 7) {                         System.out.println(&quot;You rolled &quot; + firstDice + &quot; + &quot; + secondDice +                          &quot; which is &quot; + throwSum);                                                  if (throwSum == playerPoint) {                             System.out.println(&quot;Congratulations! You win. You reached your point.&quot;);                         break;                         }                         System.out.println(&quot;Your point is &quot; + playerPoint + &quot;. Good luck.&quot;);                     }                      else {                         System.out.println(&quot;You rolled &quot; + firstDice + &quot; + &quot; + secondDice +                          &quot; which is &quot; + throwSum);                         System.out.println(&quot;Sorry, you crapped out, you lose.&quot;);                          System.out.println(&quot;You rolled 7 before reaching your point.&quot;);                         break;                     }                 }                    }             System.out.println(&quot;Do you want to play again? yes/no: &quot;);                               // even if user enters &quot;no&quot; the loop does not exit                  proceed = input.nextLine();          }         System.out.println(&quot;Thanks for playing.&quot;);         enter.close();         input.close();     } }
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
77364
I have a program that continually polls the database for change in value of some field. It runs in the background and currently uses a while(true) and a sleep() method to set the interval. I am wondering if this is a good practice? And, what could be a more efficient way to implement this? The program is meant to run at all times.  Consequently, the only way to stop the program is by issuing a kill on the process ID. The program could be in the middle of a JDBC call. How could I go about terminating it more gracefully? I understand that the best option would be to devise some kind of exit strategy by using a flag that will be periodically checked by the thread. But, I am unable to think of a way/condition of changing the value of this flag. Any ideas?
55476
Is there any condition where finally might not run in java? Thanks.
92456
Alright, I have been doing the following (variable names have been changed):   FileInputStream fis = null; try {     fis = new FileInputStream(file);      ... process ...  } catch (IOException e) {     ... handle error ... } finally {     if (fis != null)         fis.close(); }    Recently, I started using FindBugs, which suggests that I am not properly closing streams.  I decide to see if there's anything that can be done with a finally{} block, and then I see, oh yeah, close() can throw IOException.  What are people supposed to do here?  The Java libraries throw too many checked exceptions.
56680
I've seen both the blocks of code in use several different times, personally I have always used the first but my question is: is there a functional difference, and if there is what is it?  while (condition is true ) {     // do something }  do {     // do something } while ( condition is true);   I will be applying this to PHP but I assume this is fairly language agnostic.
27164
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything:  StreamReader reader=new  StreamReader("myfile.txt"); try {   int i = 5 / 0; } catch   // No args, so it will catch any exception {} reader.Close();   However, this is considered good form:  StreamReader reader=new  StreamReader("myfile.txt"); try {   int i = 5 / 0; } finally   // Will execute despite any exception {   reader.Close(); }   As far as I can tell, the only difference between putting cleanup code in a finally block and putting cleanup code after the try..catch blocks is if you have return statements in your try block (in that case, the cleanup code in finally will run, but code after the try..catch will not).  Otherwise, what's so special about finally?
41167
I am told that good developers can spot/utilize the difference between Null and False and 0 and all the other good "nothing" entities. What is the difference, specifically in PHP?  Does it have something to do with ===?
27797
I'm not the most experienced person when it comes to commands. I worked out how to do stuff like when holding x you have y effect. I wanted to think of something that gave other people an effect when hit. Here is what I came up with but I got stuck when it said it couldn't execute the command.  /scoreboard objective add nokill dummy  /scoreboard objective add poi dummy  /scoreboard players set [player] nokill 1  /scoreboard players add @a poi 1 {SelectedItem:{id:minecraft:diamond_sword,display:{Name:{Poison sword}}}}  /execute @a[score_poi_min=1] ~ ~ ~ /effect @p[score_nokill_max=1,r=5] {HurtTime:1} minecraft:poison 10 1
121
I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation?
23537
How do I stop a Java process gracefully in Linux and Windows?  When does Runtime.getRuntime().addShutdownHook get called, and when does it not?  What about finalizers, do they help here?  Can I send some sort of signal to a Java process from a shell?  I am looking for preferably portable solutions.
119736
I have a try...except block in my code and When an exception is throw. I really just want to continue with the code because in that case, everything is still able to run just fine. The problem is if you leave the except: block empty or with a #do nothing, it gives you a syntax error. I can't use continue because its not in a loop. Is there a keyword i can use that tells the code to just keep going?
2156
In Java is there a way to check the condition:  "Does this single character appear at all in string x"  without using a loop?
16499
If yes, on which operating system, shell or whatever?  Consider the following java program (I'm using java just as an example, any language would be good for this question, which is more about operation systems):  public class ExitCode {     public static void main(String args[]) {         System.exit(Integer.parseInt(args[0]));     } }   Running it on Linux and bash, it returns always values less equal 255, e.g. (echo $? prints the exit code of the previous executed command)  &gt; java ExitCode 2; echo $? 2  &gt; java ExitCode 128; echo $? 128  &gt; java ExitCode 255; echo $? 255  &gt; java ExitCode 256; echo $? 0  &gt; java ExitCode 65536; echo $? 0     EDITED: the (only, so far) answer below fully  explain what happens on UNIXes. I'm still wondering about other OSes.
45336
When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the loop. These notions were often backed up with references to studies which showed that loops which tested at the top were statistically much more likely to be correct than their bottom-testing counterparts.  As a result, I almost always write loops which test at the top. I don't do it if it introduces extra complexity in the code, but that case seems rare. I notice that some programmers tend to almost exclusively write loops that test at the bottom. When I see constructs like:  if (condition) {     do     {        ...     } while (same condition); }   or the inverse (if inside the while), it makes me wonder if they actually wrote it that way or if they added the if statement when they realized the loop didn't handle the null case.  I've done some googling, but haven't been able to find any literature on this subject. How do you guys (and gals) write your loops?
115229
We use the so-called zero conditional when the result of the condition is always true.  What about following situation. Let's say that we have a poorly written application. :) and I complain that it simply does not work. It is always true for this version of application.  Zero conditional sounds good to me. Is it wrong to use first conditional here?   "if I press a button it crashes." "if I press a button it will crash."   thanks!
45967
I'm trying to make a simple blackjack program. Sadly, I'm having problems right off the bat with generating a deck of cards.  #include &lt;iostream&gt; #include &lt;vector&gt;  using namespace std;  int main() {     vector&lt;char&gt; deck;     char suit[] = {'h','d','c','s'};     char card[] = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'};     for (int j=0; j&lt;13; j++) {         for (int i=0; i&lt;4; i++) {             deck.push_back(card[j] suit[i]);         }            }      return 0; }   I know my problem begins with me trying to assign the value '10' to a char. Obviously I couldn't get this to compile but I'm sure when I try to assign the card values to the vector deck I'll also get an error since I used variable type 'char'. Knowing what kind of variable type to use seems to be killing me. Also, would 'deck.push_back(card[j] suit[i]);' be the correct code to combine the card and suit, or do you have to put something between card[j] and suit[i]? I'd appreciate it if any of you could lead me in the right direction. Also as a little side note, this is part of a homework assignment so please don't just give me entire blocks of code. Thanks for your help.
73226
I'm having trouble restarting a 2D game when the user presses the "New Game" button.  Structure of my game:  The game has several 'screens': Opening screen, instructions screen, main game screen, and a game-over screen.  Each screen is an instance of a class: OpeningScreen class, GameScreen class, etc.  There is a class named Main. This class has three roles.   It's the JFrame (the window) of the program. It has the main method - first class to run. It has a method that change's screens.   When the program starts, this class runs. When it first runs:   Creates a new instance of itself (it's a JFrame-type class). Creates an instance of every 'screen-class'. The constructor of each 'screen-class' takes the instance of Main as an argument: (aka: GameOverScreen gameOverScreen = new GameOver(this)). Creates a new JPanel named mainPanel, and adds it to the JFrame. Any screen to be displayed will be contained in this JPanel (The screens are also JPanel-type classes).   How the changeScreen() method works:  The signature of changeScreen() is this: changeScreen(JPanel from, JPanel to).  Whenever a screen needs to be changed, the current screen that is displayed calls this method, like so: main.changeScreen(this, main.InstructionsScreen). It removes the previous screen from mainPanel and adds the new screen to mainPanel. As said, these screens are created when the program launches.  This is the method that changes screens:   public void changeScreen(JPanel from, JPanel to){      mainPanel.remove(from);     mainPanel.add(to);      mainPanel.revalidate();     mainPanel.repaint();  }   My problem:  After a game is finished, the Game class calls the changeScreen method: main.changeScreen(this, main.GameOverScreen) .  This replaces the game screen with a game over screen. Pressing the "New Game" button in the game-over screen, calls main.changeScreen(this, main.GameScreen) .  The problem: Pressing the "New Game" button in the game-over screen, indeed displays once again the instance of the Game class. But this is the old instance, so the game is not restarted. It's already finished.  I tried to do the following changes in changeScreen():  public void changeScreen(JPanel from, JPanel to){      mainPanel.remove(from);      if(to.equals(gameScreen)){         gameScreen = null;         gameScreen = new GameScreen(this);     }     mainPanel.add(gameScreen);      mainPanel.revalidate();     mainPanel.repaint();  }   This should reset the gameScreen object. But this creates a delay of about 10 seconds when pressing the "New Game" button.  What would be a good way of restarting the game?  Thank you