My class implements a very simple prototype of an RPN calculator.  The following construct isn't working. Why? What am I doing wrong here?  public boolean executeCommand(String command) {     if(command == "+")      { add();          return true; }else     if(command == "-")      { subtrair();     return true; }else     if(command == "*")      { multiplicar();  return true; }else     if(command == "/")      { dividir();      return true; }else         {             System.out.println("The command does not exist.");             return false;         } }   The output is always, no matter what the string contains,  The command does not exist.   Why? I really don't get it! If someone could please explain, I'd be thankful!    In more detail  The method in question is:  public boolean executeCommand(String command) {     Scanner str = new Scanner(command);      if (str.hasNextDouble()) {         dataStack.push(str.nextDouble());         return true;     } else {          System.out.format(" DEBUG: command: %s$%n", command);          if(command == "+")      { add();          return true; }else         if(command == "-")      { subtract();     return true; }else         if(command == "*")      { multiply();     return true; }else         if(command == "/")      { divide();       return true; }else         if(command == ".")      { print();        return true; }else         if(command == ".s")     { showStack();    return true; }else         if(command == "exit")   { exit();         return true; }else             {                 System.out.println("The command does not exist.");                 return false;             }     } }   which, for any input I threw at it, (except for numbers, of course), resulted in:   DEBUG: command: [COMMAND HERE]$ The command does not exist.     Source code  I removed some irrelevant source code; (i.e. some methods, package name) but it is still compilable and runnable:  import java.util.Scanner; import java.util.LinkedList;  public class RPNCalculator {      public static void main(String[] args) {         RPNCalculator calc = new RPNCalculator();         calc.startInteractiveMode();     }      protected Scanner scanInput;     public LinkedList&lt;Double&gt; dataStack;     protected boolean interactiveModeEnabled;      public RPNCalculator() {         scanInput = new Scanner(System.in).useDelimiter("\\s+");         dataStack = new LinkedList&lt;Double&gt;();     }      public String getCommand() {         return scanInput.next();     }      public boolean executeCommand(String command) {         Scanner str = new Scanner(command);          if (str.hasNextDouble()) {             dataStack.push(str.nextDouble());             return true;         } else {              System.out.format(" DEBUG: command: %s$%n", command);              if(command == "+")   { ommitedOp("add");      return true; }else             if(command == "-")   { ommitedOp("subtract"); return true; }else             if(command == "*")   { ommitedOp("multiply"); return true; }else             if(command == "/")   { ommitedOp("divide");   return true; }else             if(command == ".")   { ommitedOp("print");    return true; }else             if(command == ".s")  { ommitedOp("showStack");return true; }else             if(command == "exit"){ ommitedOp("exit");     return true; }else                 {                     System.out.println("The command does not exist.");                     return false;                 }         }     }      public void startInteractiveMode() {         interactiveModeEnabled = true;          while (interactiveModeEnabled) {             String command = getCommand();             executeCommand(command);         }     }      public void ommitedOp(String method){         System.out.println("Command exists!");     } }
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?
22377
Some time ago, I came across a piece of code, that used some piece of standard Java functionality to locate the classes that implemented a given interface. I know the functions were hidden in some non-logical place, but they could be used for other classes as the package name implied. Back then I did not need it, so I forgot about it, but now I do, and I can't seem to find the functions again. Where can these functions be found?  Edit: I'm not looking for any IDE functions or anything, but rather something that can be executed within the Java application.
79610
an example code what I try to do will surely do better than my english:  public bool IsNumericValueInBounds (string value, Type numericType) {   double d = double.NaN;         bool inBounds = (bool)numericType.GetMethod ("TryParse").Invoke (null, new object[] { value, d });    return inBounds; }   Unfortunately the TryParse method needs an out parameter so this doesn't work. any ideas how to solve this?  (ps.: would'nt this be a nice example for duck typing? - because i know every numericType has an "TryParse" or I am mistaken?)
116765
I'm trying to come up with a command which should increase a counter and return a string including the current value of the counter.  Have a look at the following minimal example:  \documentclass{article}  \newcounter{mycounter} \newcommand{\mystepFAILS}{\stepcounter{mycounter}foo\themycounter} \newcommand{\mystepWORKS}{foo\themycounter}  \begin{document}  \section{\mystepFAILS}  \section{\mystepWORKS}  \end{document}   Why does the command with \mystepFAILS throw an error? What is so special about \stepcounter? Any ideas?
77306
When run a command, if the command is not present, then some information regarding the failure of the command is displayed.  I am trying to take the information about that failed command as input to my script, which has to run automatically whenever the command fails.  Whenever the command fails the $? value would be 127. I have to catch this failed event and run my commands there.
52524
So I was making a class the other day and used Eclipse's method to create the equals method when I realized that it generated the following working code:  class Test {   private int privateInt;   [...]   public boolean equals(Object obj) {     [...]     Test t = (Test) obj;     if ( t.privateInt == privateInt ) {     [...]   } }   t.privateInt..???? It's suppose to be private! So I guess there is one more field visibility other than private, protected, package protected and public.  So what is happening here? How is this called? Where would somebody use this? Doesn't this break encapsulation? What if the class didn't have a mutator and I changed this? Does this happen to C++ as well? Is this an OO idiom? If not, then why did Java do it?  Generally, where can I find information about this?  Thank you.
13332
I am trying to use a  as input and then creating a query with it - so I need a .    I was surprised to find that it couldn't do the conversion implicitly or explicitly - but I don't even know how I would do this, as the Java API is still fairly new to me.
30627
In C# the Main class has string[] args parameter.  What is that for and where does it get used?
92088
public class WrapperTest {      public static void main(String[] args) {          Integer i = 100;         Integer j = 100;          if(i == j)             System.out.println("same");         else             System.out.println("not same");     }     }   The above code gives the output of same when run, however if we change the value of i and j to 1000 the output changes to not same. As I'm preparing for SCJP, need to get the concept behind this clear. Can someone explain this behavior.Thanks.
56379
I'm writing a wrapper class for a command line executable.  This exe accepts input from stdin until I hit Ctrl+C in the command prompt shell, in which case it prints output to stdout based on the input.  I want to simulate that Ctrl+C press in C# code, sending the kill command to a .NET Process object.  I've tried calling Process.Kill(), but that doesn't seem to give me anything in the process's StandardOutput StreamReader.  Might there be anything I'm not doing right?  Here's the code I'm trying to use:  ProcessStartInfo info = new ProcessStartInfo(exe, args); info.RedirectStandardError = true; info.RedirectStandardInput = true; info.RedirectStandardOutput = true; info.UseShellExecute = false; Process p = Process.Start(info);  p.StandardInput.AutoFlush = true; p.StandardInput.WriteLine(scriptcode);  p.Kill();  string error = p.StandardError.ReadToEnd(); if (!String.IsNullOrEmpty(error))  {     throw new Exception(error); } string output = p.StandardOutput.ReadToEnd();   The output is always empty, even though I get data back from stdout when I run the exe manually.  Edit: This is C# 2.0 by the way.
70228
I've found a number of answers online, and plainly none of them have seemed to be able to execute any conditional code.  answer looked particularly promising.  One thing I noticed is that in the command line help, xetex (and similarly pdflatex) claim that the COMMANDS part of      xetex [OPTION]... [TEXNAME[.tex]]   [COMMANDS]   is latex code that will be processed after the file TEXNAME.tex. Now it occurs to me that this would cause any conditionals which rely on the value of the flags set in COMMANDS to fail, due to the processor not finding the flag value until the end of the file, at which point it is too late. How do I get a command line argument to be interpretted as xetex/latex code which is to be inserted into the document at some point before the \begin{document} element?  The specific problem that I have, is that I want a document that may contain hyperlinks to different parts of the document, but only if I flag it as an electronic copy upon compilation.
83164
How can I write a program in Java that will execute another program? Also, the input of that program should be given from our program and the output of that program should be written into a file.  This is my small set of code to get its output:  Process p = Runtime.getRuntime().exec("C:\\j2sdk1.4.0\bin\\helloworld.java"); BufferedReader input =         new BufferedReader(new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null)    System.out.println(line);  input.close();   This was my set of code but this throws an IOException.
37488
This works just fine:      protected void txtTest_Load(object sender, EventArgs e)     {         if (sender is TextBox) {...}      }   Is there a way to check if sender is NOT a TextBox, some kind of an equivalent of != for "is"?  Please, don't suggest moving the logic to ELSE{} :)
31203
Is there a package that provides a command for testing whether a string is in a list?  I saw several answers that define their own commands to perform this or similar tasks, but I prefer a "self-evolving", and well-documented solution for this kind of tasks.  More precisely, the command should be something like this  \IfStringInList{string}{list}{DoThisIfTrue}{DoThisIfFalse}   For instance  \IfStringInList{Paul}{George,John,Paul,Ringo}{Beat it}{Roll it}   EDIT:  I'm not against defining commands.  I expected thas this natural functionality was present in a ready-to-use package, that can be accessed in a very simple way.
107801
I posted the code below to the Sun developers forum since I thought it was erroring (the true error was before this code was even hit).  One of the responses I got said it would not work and to throw it away.  But it is actually working.  It might not be the best code (I am new to Java) but is there something inherently "wrong" with it?   =============  CODE:   private static void ImportFromCsvToAccessTable(String mdbFilePath, String accessTableName  , String csvDirPath , String csvFileName ) throws ClassNotFoundException, SQLException {      Connection msConn = getDestinationConnection(mdbFilePath);     try{          String strSQL = "SELECT * INTO " + accessTableName + " FROM [Text;HDR=YES;DATABASE=" + csvDirPath + ";].[" + csvFileName + "]";         PreparedStatement selectPrepSt = msConn.prepareStatement(strSQL );         boolean result = selectPrepSt.execute();                 System.out.println( "result = " + result );      } catch(Exception e) {         System.out.println(e);     } finally {         msConn.close();     } }
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?
109620
I used   Runtime.getRuntime().exec("_____")   but it throws a IOException as below:  java.io.IOException: CreateProcess: c:/ error=5   at java.lang.Win32Process.create(Native Method)   at java.lang.Win32Process.&lt;init&gt;(Win32Process.java:63)   at java.lang.Runtime.execInternal(Native Method   I don't know whether I have the problem with specifying the path or something else. Can anyone please help me with the code.