I'm a new programmer and trying to teach myself Java by doing random projects. Below is a "Rock, Paper, Scissors" game and the issue that I'm facing is after printing "a", the program ends and does not continue onto the if else statements below. Any help that can be given would be greatly appreciated.  package com.company;  import java.util.Scanner;  public class Main {      public static void main(String[] args) {          System.out.println("Hello &amp; Welcome to Rock, Paper, Scissors. What's your name?");         Scanner scan = new Scanner(System.in);         String userChoice = scan.nextLine();         System.out.println("Hello, " + userChoice + ". Let's start a game!");         Scanner scan = new Scanner(System.in);         System.out.println("Choose one: Rock, Paper, Scissors");         String userFirstChoice = scan.nextLine();         System.out.println("You chose " + userFirstChoice);          double a = Math.random();         System.out.println(a);            if (a &gt;= 0.00 &amp;&amp; a&lt;= 0.3){             if ( userFirstChoice== "Rock"){                 System.out.println("Rock vs Rock: TIE");             }             else if (userFirstChoice == "Paper"){                 System.out.println("Rock vs Paper: YOU LOSE!");             }             else if (userFirstChoice == "Scissors"){                 System.out.println("Rock vs Scissors: YOU WIN!");             }          }         else if (a&gt;=0.3 &amp;&amp; a&lt;=0.6){             if(userFirstChoice == "Paper"){                 System.out.println("Paper vs Paper: TIE!");             }             else if (userFirstChoice == "Rock"){                 System.out.println("Rock vs Paper: YOU LOSE!");             }             else if(userFirstChoice == "Scissors"){                 System.out.println("Scissors vs Paper: YOU WIN!");             }          }         else if (userFirstChoice == "Scissors"){             System.out.println("Scissors vs Scissors: TIE!");         }         else if (userFirstChoice == "Paper"){             System.out.println("Paper vs Scissors: YOU LOSE!");         }         else if (userFirstChoice == "Rock") {              System.out.println("Rock vs Scissors: YOU WIN!");         }      }   }
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?
24218
Is this functionality going to be put into a later Java version?  Can someone explain why I can't do this, as in, the technical way Java's switch statement works?
60003
I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just the current thread. Are there any common patterns for dealing with this? For example, can I subsitute a stub for System.exit()?   [EDIT] The class in question is actually a command-line tool which I'm attempting to test inside JUnit. Maybe JUnit is simply not the right tool for the job? Suggestions for complementary regression testing tools are welcome (preferably something that integrates well with JUnit and EclEmma).
40162
Please demonstrate how the ternary operator works with a regular if/else block. Example:  Boolean isValueBig = value &gt; 100 ? true : false;     Exact Duplicate:
24489
#include &lt;stdio.h&gt; #include &lt;conio.h&gt; main() {     char ch,name[20];     int i=0;     clrscr();     printf("Enter a string:");     while((ch=getch())!='\n')     {         name[i]=ch;         i++;     }     name[i] = '\0';     printf("%s",name); }   When I give "abc" as input and if I press enter it's not working. Can anyone let me know why the condition ch=getch() != '\n' is not becoming false when I press enter? I have also observed that ch is taking \r instead of \n. Kindly let me know. Thanks
111151
Can I simply use  if(myString == nil)   For some reason a string that I know is null, is failing this statement.
16116
When using IF statements in Python, you have to do the following to make the "cascade" work correctly.  if job == "mechanic" or job == "tech":         print "awesome" elif job == "tool" or job == "rock":         print "dolt"   Is there a way to make Python accept multiple values when checking for "equals to"? For example,  if job == "mechanic" or "tech":     print "awesome" elif job == "tool" or "rock":     print "dolt"
69342
I'm using this code to let the user enter in names while the program stores them in an array until they enter an empty string (they must press enter after each name):  people = [] info = 'a' # must fill variable with something, otherwise loop won't execute  while not info.empty?     info = gets.chomp     people += [Person.new(info)] if not info.empty? end   This code would look much nicer in a do ... while loop:  people = []  do     info = gets.chomp     people += [Person.new(info)] if not info.empty? while not info.empty?   In this code I don't have to assign info to some random string.  Unfortunately this type of loop doesn't seem to exist in Ruby. Can anybody suggest a better way of doing this?
453
I need to write a Java Comparator class that compares Strings, however with one twist.  If the two strings it is comparing are the same at the beginning and end of the string are the same, and the middle part that differs is an integer, then compare based on the numeric values of those integers.  For example, I want the following strings to end up in order they're shown:   aaa bbb 3 ccc bbb 12 ccc ccc 11 ddd eee 3 ddd jpeg2000 eee eee 12 ddd jpeg2000 eee   As you can see, there might be other integers in the string, so I can't just use regular expressions to break out any integer.  I'm thinking of just walking the strings from the beginning until I find a bit that doesn't match, then walking in from the end until I find a bit that doesn't match, and then comparing the bit in the middle to the regular expression "[0-9]+", and if it compares, then doing a numeric comparison, otherwise doing a lexical comparison.  Is there a better way?  Update I don't think I can guarantee that the other numbers in the string, the ones that may match, don't have spaces around them, or that the ones that differ do have spaces.
19207
So my 2009 new years resolution is to learn Java.  I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse.  Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.  Here is the sample code:  import java.util.Scanner; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.GridLayout;  class TeamFrame extends JFrame {      public TeamFrame() throws IOException {         PlayerPlus player;         Scanner myScanner = new Scanner(new File("Hankees.txt"));          for (int num = 1; num &lt;= 9; num++) {             player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble());             myScanner.nextLine();             addPlayerInfo(player);         }                add(new JLabel());         add(new JLabel("   ------"));         add(new JLabel("Team Batting Aberage:"));         add(new JLabel(PlayerPlus.findTeamAverageString()));         setTitle("The Hankees");         setLayout(new GridLayout(11,2));         setDefaultCloseOperation(EXIT_ON_CLOSE);         pack();         setVisible(true);     }      void addPlayerInfo(PlayerPlus player) {         add(new JLabel(player.getName()));         add(new JLabel(player.getAverageString()));     } }   And you can see in the below screen shot I have included this file.  image no longer available  Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.  Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.  Scanner myScanner = new Scanner(new File("C:\\Hankees.txt"));   So basically, my question is what am I doing wrong?  Or is Eclipse responsible for this in some way?  Thanks for any and all help!
78122
Please take a look on the following pseudo-code:  boolean blocked[2]; int turn; void P(int id) {       while(true) {              blocked[id] = true;              while(turn != id) {                     while(blocked[1-id])                     /* do nothing */;                     turn = id;              }              /* critical section */              blocked[id] = false;              /* remainder */       } } void main() {       blocked[0] = false;       blocked[1] = false;       turn = 0;       parbegin(P(0), P(1)); //RUN P0 and P1 parallel }   I thought that a could implement a simple Mutual - Exclution solution using the code above. But it's not working. Has anyone got an idea why?  Any help would really be appreciated!
382
I'm reviewing some code for a friend and say that he was using a return statement inside of a try-finally block.  Does the code in the Finally section still fire even though the rest of the try block doesn't?  Example:  public bool someMethod() {   try   {     return true;     throw new Exception("test"); // doesn't seem to get executed   }   finally   {     //code in question   } }
31170
i am instantiating prefab cars ... the clones had a random countdown timer with each one off them and its working very well but the its not  decreasing its not counting down the timer is inside the spawn method the spawn method is inside the WHILE loop and the while loop is inside the UpDate() ... i  tried  to move the countdown to Update() but didnn't worked ... any ideas ...Thank You!  void Update() {     p -= Time.deltaTime;           while (carNumber &lt; cars &amp;&amp; p &lt;= 0 )         {              SpawnCars();             print(carNumber);             p = 5;                    }  }   and this is the spawn method:   public void SpawnCars() {     if (spawnAllowed)     {         // choose a random strat point 1/6              randomSpawnPoint = Random.Range(0, spawnPoints.Length);         // choose a random car 1/6         randomSpawnCars = Random.Range(0, Cars.Length);         // clone randomly a car(1/6) from a random pre_set-points(1/6)         GameObject obj = Instantiate(Cars[randomSpawnCars], spawnPoints[randomSpawnPoint].position, Quaternion.Euler(-90, 180, 0)) as GameObject;         // carNumber will count how many cloned car          carNumber++;         // destroy the  cloned car after N sec        // Destroy(obj, 8);         // every cloned car will take a tag with it         obj.gameObject.tag = "select" + k;         k++;         rcf = obj.GetComponent&lt;RayCastForward&gt;();         //textCDT         time = Random.Range(4f, time);         TextMesh txt = FindObjectOfType&lt;TextMesh&gt;();   //the Timer:         time -= Time.deltaTime;         coolDown = time.ToString("0");         txt.text = ""+coolDown;      } }   edit: i forgot to tell that the timer  is inside SpawnCars().
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.
116653
I have this:  If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then     stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value End If   Now, when editTransactionRow.pay_id is Null Visual Basic throws an exception. Is there something wrong with this code?
81862
Does string immutability work by statement, or by strings within a statement?   For example, I understand that the following code will allocate two strings on the heap.  string s = "hello "; s += "world!";   "hello" will remain on the heap until garbage collected; and s now references "hello world!" on the heap. However, how many strings does the following line allocate on the heap...1 or 2? Also, is there a tool/way to verify the results?  string s = "goodbye " + "cruel world!";
27214
How can I return to the start of a line and overwrite what has already been output on the console? The following does not appear to work:  System.out.print(mystuff+'\r');