public class cripto {        static String chiave, mex;     static int lungC,lungM, lungAl,lungCh;     static String[] aChiave, aMex;     static String[] alfabeto = {"a", "b", "c","d", "e", "f","g", "h", "i","l", "m", "n","o", "p", "q","r", "s", "t","u", "v", "z"};     public static void main(String[] args) {         // TODO Auto-generated method stub      chiave=JOptionPane.showInputDialog(null, "Inserisci chiave di criptografia di lunghezza: "+alfabeto.length);     System.out.println("chiave: "+chiave);     lungC=chiave.length();     for (int i=0; i&lt;lungC; i++){         aChiave=chiave.split("");         System.out.println(i+": "+aChiave[i]);     }     mex=JOptionPane.showInputDialog(null, "Inserisci messaggio");     System.out.println("messaggio: "+mex);     lungM=mex.length();     for (int i=0; i&lt;lungM; i++){         aMex=mex.split("");         System.out.println(i+": "+aMex[i]);     }     lungAl=alfabeto.length;     lungCh=aChiave.length;     System.out.println("Lunghezza array alfabeto: "+lungAl);     System.out.println("Lunghezza array chiave: "+lungCh);     String[] aCripto=new String[lungM];      if(aChiave.length==alfabeto.length){         for(int i=0;i&lt;lungM;i++){             for(int j=0;j&lt;alfabeto.length;j++){                 //System.out.println(i+": "+h+" - "+h1);                 if(aMex[i]==alfabeto[j]){                     aCripto[i]=aChiave[j];                 }             }         }     }else{         JOptionPane.showMessageDialog(null, "Lunghezze diverse");     } }   }  Hy guys, I Made this simple criptography's program but I have the problem that in the for, the program doesn't enter in the "if" even if the equality is verified.                   if(aMex[i]==alfabeto[j]){                     aCripto[i]=aChiave[j];                 }   can someone help me? sorry for my bad english.
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?
109679
I have the following code:      public class OrderRequestCreationEmails {     public static void sendMail()     {          Messaging.reserveSingleEmailCapacity(2);         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();         String[] toAddresses = new String[] {'dgriff@varonis.com'};           mail.setToAddresses(toAddresses);          mail.setReplyTo('dgriff@varonis.com');          mail.setSenderDisplayName('Mail Test');          mail.setSubject('Success! Mail Sent');          mail.setBccSender(false);         mail.setPlainTextBody('BODY');          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });     } }   I tried to call this function from a test as such: (I know its not an actual test, its just to see if I receive a mail).  @isTest private class CreationEmailsTest {     @isTest private static void sendMailTest() {         OrderRequestCreationEmails.sendMail();     } }   And I haven't received the mail, no idea why?  It's an example I took from the salesforce developer guide. couldn't find any answers online...  FYI - deliverability in the sandbox is turned on, and test deliverability works
11190
When building console applications that take parameters, you can use the arguments passed to Main(string[] args).   In the past I've simply indexed/looped that array and done a few regular expressions to extract the values. However, when the commands get more complicated, the parsing can get pretty ugly.    So I'm interested in:   Libraries that you use Patterns that you use   Assume the commands always adhere to common standards such as .
34586
I needed some simple string encryption, so I wrote the following code (with a great deal of "inspiration" from ):      // create and initialize a crypto algorithm     private static SymmetricAlgorithm getAlgorithm(string password) {         SymmetricAlgorithm algorithm = Rijndael.Create();         Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(             password, new byte[] {             0x53,0x6f,0x64,0x69,0x75,0x6d,0x20,             // salty goodness             0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65         }         );         algorithm.Padding = PaddingMode.ISO10126;         algorithm.Key = rdb.GetBytes(32);         algorithm.IV = rdb.GetBytes(16);         return algorithm;     }      /*       * encryptString      * provides simple encryption of a string, with a given password      */     public static string encryptString(string clearText, string password) {         SymmetricAlgorithm algorithm = getAlgorithm(password);         byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);         MemoryStream ms = new MemoryStream();         CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);         cs.Write(clearBytes, 0, clearBytes.Length);         cs.Close();         return Convert.ToBase64String(ms.ToArray());     }      /*      * decryptString      * provides simple decryption of a string, with a given password      */     public static string decryptString(string cipherText, string password) {         SymmetricAlgorithm algorithm = getAlgorithm(password);         byte[] cipherBytes = Convert.FromBase64String(cipherText);         MemoryStream ms = new MemoryStream();         CryptoStream cs = new CryptoStream(ms, algorithm.CreateDecryptor(), CryptoStreamMode.Write);         cs.Write(cipherBytes, 0, cipherBytes.Length);         cs.Close();                     return System.Text.Encoding.Unicode.GetString(ms.ToArray());     }   The code appears to work fine, except that when decrypting data with an incorrect key, I get a CryptographicException - "Padding is invalid and cannot be removed" - on the cs.Close() line in decryptString.   example code:      string password1 = "password";     string password2 = "letmein";     string startClearText = "The quick brown fox jumps over the lazy dog";     string cipherText = encryptString(startClearText, password1);     string endClearText = decryptString(cipherText, password2);     // exception thrown   My question is, is this to be expected? I would have thought that decrypting with the wrong password would just result in nonsense output, rather than an exception.
20332
i just made a test, to see how much data i can put in a string before i reach the the heapsize limit and i get an unexpected behaviour   string  csvFile = '"XXXXXXX","XXPX XXRXXXX","XXPX BP","XLXXXXR","XXXXVXXX PRXXXDXXXX","XXPX XXXXVXXX","XDRXXXDXX LXGXLX","XXXXRX XXLX","LXXXX XX","DXXX XXXZXX XXXXVXXX","DXXX XXXX XXXXVXXX","XXDXXX BP","XXDXXX XXXXXLX","PXRXXXX XVX","DXXXXXXXZXXXX","XXXX XXRXXXXRX","XXGXXXX XXRXXXXRX","DXXX DX XXXXXXX","LXXGX DX XXXXXXX","PXD","XXXXX XXXXRXXXXXLX","XXXX RXXXPXXX","XXDXRXZZX RXXXPXXX","PRXVXXXXX RXXXPXXX","XXXXXX RXXXPXXX","XXP RXXXPXXX","XXXXRX XXLXXXXX 1","XXXXRX XXLXXXXX 2","XXXXRX XXX","XXDXRXZZX XXXXL","XXDXRXZZX XXRXXXXRX","XXXXXX XXRXXXXRX","PRXVXXXXX XXRXXXXRX","XXP XXRXXXXRX","XXXXX XLXXXXX","XXDXXX PRXXXXX","DXXX XXXXXZXXXX","XXPX DXXXXXXXX","XHXXVX XXXXXRX","DXXX XXXXXXXXX XXXXXRX","DXXX XXXDXXZX XXXXXRX","XXPXRXX XXXXLX","XXPXRXX RXXXDXX XXXXDXXX","PRXXXXXXXXXXXXXXZXXXX PRXXXXXXXXXX","DXXX PRXXXXXXXXXXXXXXZXXXX PRXXXXXXXXXX"';  string  csvFile2='',csvFile3; system.debug(limits.getLimitHeapSize()); system.debug(limits.getHeapSize()); for(integer i=0;i&lt;3618;i++){ csvFile2+='\n'+csvFile; }   system.debug(1.0*limits.getHeapSize()); system.debug((1.0*limits.getHeapSize())/limits.getLimitHeapSize()*100);   with 3618 iteraction i get    Heap size 3001245.0 Which correspond to ~50% of the heap size limit in the Synchronous context   but if i go to 3619 iteractions in the for, i get the error "Apex heap size too large: 6001368".... which surprise me a lot...   I'm not getting out why this behaviour is occurring. Do you have any similar experience? There is a reason for this?  Seems more about limits on strings, in effect the string csvFile2 overcome 3M characters... but i dont know
13470
I am a little surprised by the following.  Example 1:  char s[100] = "abcd"; // declare and initialize - WORKS   Example 2:  char s[100]; // declare s = "hello"; // initalize - DOESN'T WORK ('lvalue required' error)   I'm wondering why the second approach doesn't work. It seems natural that it should (it works with other data types)? Could someone explain me the logic behind this?
105396
What is the difference between  char str1[32] = "\0";   and   char str2[32] = "";
119860
A weird bug was occurring in production which I was asked to look into. The issue was tracked down to a couple of variables being declared within a For loop and not being initialized on each iteration. An assumption had been made that due to the scope of their declaration they would be &quot;reset&quot; on each iteration. Could someone explain why they would not be)? (My first question, really looking forward to the responses.) The example below is obviously not the code in question but reflects the scenario:  Please excuse the code example, it looks fine in the editor preview?? for (int i =0; i&lt; 10; i++) {     decimal? testDecimal;     string testString;      switch( i % 2  )     {         case 0:         testDecimal = i / ( decimal ).32;         testString = i.ToString();             break;         default:             testDecimal = null;             testString = null;             break;     }      Console.WriteLine( &quot;Loop {0}: testDecimal={1} - testString={2}&quot;, i, testDecimal , testString ); }   EDIT: Sorry, had to rush out for child care issue. The issue was that the prod code had was that the switch statement was huge and in some &quot;case&quot;'s a check on a class' property was being made, like if (myObject.Prop != null) then testString = myObject.Stringval... At the end of the switch, (outside) a check on testString == null was being made but it was holding the value from the last iteration,hence not being null as the coder assumed with the variable being declared within the loop. Sorry if my question and example was a bit off, I got the phone call about the day care as I was banging it together. I should have mentioned I compared IL from both variables in and out the loop. So, is the common opinion that &quot;obviously the variables would not be reinitialized on each loop&quot;? A little more info, the variables WHERE being initialized on each iteration until someone got over enthusiastic with ReSharper pointing out &quot;the value is never used&quot; and removed them.  EDIT: Folks, I thank you all. As my first post I see how much clearer I should be in the future. The cause of our unexpected variable assignment can me placed on an inexperienced developer doing everything ReSharper told him and not running any unit tests after he ran a &quot;Code Cleanup&quot; on an entire solution. Looking at the history of this module in VSS I see variables Where declared outside of the loop and where initialized on each iteration. The person in question wanted his ReSharper to show &quot;all green&quot; so &quot;moved his variables closer to assignment&quot; then &quot;Removed redundant assignment&quot;! I don't think he will be doing it again...now to spend the weekend running all the unit tests he missed! How to do mark a question as answered?
97749
C#,  a String's Split() method, how can I put the resulting string[] into an ArrayList or Stack?
5691
Is there a better way of getting this result? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something)  static String intToString(int num, int digits) {     StringBuffer s = new StringBuffer(digits);     int zeroes = digits - (int) (Math.log(num) / Math.log(10)) - 1;      for (int i = 0; i &lt; zeroes; i++) {         s.append(0);     }     return s.append(num).toString(); }
7406
Java has a convenient split method:  String str = "The quick brown fox"; String[] results = str.split(" ");   Is there an easy way to do this in C++?
78162
I collect a few corner cases and  and would always like to hear more. The page only really covers C# language bits and bobs, but I also find core .NET things interesting too. For example, here's one which isn't on the page, but which I find incredible:  string x = new string(new char[0]); string y = new string(new char[0]); Console.WriteLine(object.ReferenceEquals(x, y));   I'd expect that to print False - after all, "new" (with a reference type) always creates a new object, doesn't it? The specs for both C# and the CLI indicate that it should.  Well, not in this particular case. It prints True, and has done on every version of the framework I've tested it with. (I haven't tried it on Mono, admittedly...)  Just to be clear, this is only an example of the kind of thing I'm looking for - I wasn't particularly looking for discussion/explanation of this oddity. (It's not the same as normal string interning; in particular, string interning doesn't normally happen when a constructor is called.) I was really asking for similar odd behaviour.  Any other gems lurking out there?
4525
To be specific, I was trying this code:  package hello;  public class Hello {      Clock clock = new Clock();      public static void main(String args[]) {         clock.sayTime();     } }   But it gave the error      Cannot access non-static field in static method main   So I changed the declaration of clock to this:  static Clock clock = new Clock();   And it worked. What does it mean to put that keyword before the declaration? What exactly will it do and/or restrict in terms of what can be done to that object?
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.
57093
I think there's a lot for me to learn about data types. Why this happens  double result = ((3/8)*100).ToString();   it gives zero .. should be 37,5 ... :(
80627
I have a string variable that represents the name of a custom class. Example:   string s = "Customer";   I will need to create an arraylist of customers. So, the syntax needed is:  List&lt;Customer&gt; cust = new ..    How do I convert the string s to be able to create this arraylist on runtime?
8697
While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator.  For example:  String s; ... s = new String("Hello World");   This, of course, compared to  s = "Hello World";   I'm not familiar with this syntax and have no idea what the purpose or effect would be.  Since String constants typically get stored in the constant pool and then in whatever representation the JVM has for dealing with String constants, would anything even be allocated on the heap?