I'm trying to achieve something for a small personal bank account project that I'm practicing with. I want to make it so that if a user enters a space , or doesn't enter anything at all (meaning they pressed enter without entering a value) that the default name of the account switches to "Default".  Like so:  Scanner read = new Scanner(System.in); System.out.print("Enter your account's name: "); String input = read.next(); String name = "";  if (input == "" || input == null) {     name = "Default"; } else {     name = input; }   Probably a stupid question, but this is one where I'm having a little trouble on. If I enter a space and then press enter, it doesn't continue to set the name. If I press enter when it prompts for an input, it doesn't continue to set the name. Is there a way to set it?  Edit: I don't want to know whether or not a String equals another string via .equals() (thank you for pointing that out, by the way). The scanner still does not accept blank inputs.
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?
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.
97576
I have this code that checks that the string contains only space and hash characters (“#”), if so it echos 'yes' if not echos 'no' string=&quot;###############         #             #         # #############         #             #         # ######### # #         #         # # #         # ### ##### # #         #   #     # # #         # # ###########         # #                     ###############&quot;                          confirm_variable=&quot;Yes&quot;  for (( i=0; i&lt;${#string}; i++ )); do     str=&quot;${string:$i:1}&quot;     if [ &quot;$str&quot; == &quot;#&quot; ] || [ &quot;$str&quot; == &quot; &quot; ] || [ &quot;$str&quot; == &quot;\n&quot; ] ;          then             continue         else             confirm_variable=&quot;No&quot;             break     fi  done echo $confirm_variable  I am not sure why this isn't working as if I make the string equal something like: string=&quot;## #### # #  #&quot;   it seems to work fine.
78827
My brother recently started learning C++. He told me a problem he encountered while trying to validate input in a simple program. He had a text menu where the user entered an integer choice, if they entered an invalid choice, they would be asked to enter it again (do while loop). However, if the user entered a string instead of an int, the code would break. I read various questions on stackoverflow and told him to rewrite his code along the lines of:    #include&lt;iostream&gt; using namespace std;  int main() {     int a;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }     }while(true);     return 0; }   While this worked ok, I also tried a few other ideas: 1. Using a try catch block. It didn't work. I think this is because an exception is not raised due to bad input. 2. I tried if(! cin){//Do Something} which didn't work either. I haven't yet figured this one out. 3. Thirdly, I tried inputting a fixed length string and then parsing it. I would use atoi(). Is this standards compliant and portable? Should I write my own parsing function? 4. If write a class that uses cin, but dynamically does this kind of error detection, perhaps by determining the type of the input variable at runtime, would it have too much overhead? Is it even possible?  I would like to know what is the best way to do this kind of checking, what are the best practices?  I would like to add that while I am not new to writing C++ code, I am new to writing good standards compliant code. I am trying to unlearn bad practices and learn the right ones. I would be much obliged if answerers give a detailed explanation.  EDIT: I see that litb has answered one of my previous edits. I'll post that code here for reference.  #include&lt;iostream&gt; using namespace std;  int main() {     int a;     bool inputCompletionFlag = true;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }         else         {             inputCompletionFlag = false;         }     }while(!inputCompletionFlag);     return 0; }   This code fails on input like "1asdsdf". I didn't know how to fix it but litb has posted a great answer. :)
22356
Comparing string in C# is pretty simple.  In fact there are several ways to do it.  I have listed some in the block below.  What I am curious about are the differences between them and when one should be used over the others?  Should one be avoided at all costs?  Are there more I haven't listed?    string testString = "Test"; string anotherString = "Another";  if (testString.CompareTo(anotherString) == 0) {} if (testString.Equals(anotherString)) {} if (testString == anotherString) {}   (Note: I am looking for equality in this example, not less than or greater than but feel free to comment on that as well)
5349
I'm using reflection to loop through a Type's properties and set certain types to their default.  Now, I could do a switch on the type and set the default(Type) explicitly, but I'd rather do it in one line.  Is there a programmatic equivalent of default?
104445
I am wondering because I am trying to make sure that person have right amount of spaces for their user name.  User Name and Password are security measures and should not be easy to guess. I have fixed amount of characters to Passwords no shorter than 6 characters because most sites use 6-8 characters when create an account. Because of limitation of my progams database file I cannot have passwords longer than 20 characters. What about User Name field? Too short User Name would be too easy to guess, too long, might cause program errors (since maximum length of single line TextBox in .Net/C# is about 32767 characters).  What should be the maximum and minimum length of User Name and Password fields?
108596
I have a custom object that I want to set a unique key based on on lookup values. When I tried to create a workflow update I was blowing past the size limit so I developed it into a trigger and it seems to be working as intended.   I thought since I am already developing a field update trigger, I might as well add on a default name method so everything is contained into one file as opposed to having workflow and triggers updating the record.   When I introduced this method it returns "null null".   public static void setRecordName(List&lt;Responsible_Party__c&gt; newList) {     for(Responsible_Party__c r : newList)     {         r.Name = r.Practice__r.Name + ' ' + r.User__r.Name;     } }   Where practice is a lookup to another custom object and user is a lookup to the user object.   If I change the values to just use the field:  public static void setRecordName(List&lt;Responsible_Party__c&gt; newList) {     for(Responsible_Party__c r : newList)     {         r.Name = r.Practice__c + ' ' + r.User__c;     } }   I get "a1Gg00000049v9xEAA 005A0000006ykwnIAA". How can I get the names to display and not null?
92333
if ('text'.equals(str))   or   if ('text' == str)   Is there any difference? If yes, then what is it?   Added, after answered.  String q = 'teXt'; system.debug('teXt'.equals(q)); // true system.debug('teXt' == q);      // true system.debug('text'.equals(q)); // false system.debug('text' == q);      // true
40938
After I posted this question, I found a similar post that seemed to answer it: . I'll leave this in place for now, as I am not sure how to mark my own question as a duplicate  In attempting to debug a SOQL query error, I encountered a strange behavior with Database.query and bind variables. Specifically, when I run this code:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } Database.query(userQuery);   the Database.query statement executes with no errors, despite the fact that the variable firstNames does not exist in its context. However, when I add an else clause:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } else {     List&lt;String&gt; firstNames; } Database.query(userQuery);   I receive a null object reference exception. It seems like Salesforce is checking to see if the variable is only declared once beforehand and, if so, uses its value in the context it was declared. This seems strange to me, so I was wondering if anybody here has any insight into what is going on under the hood.
104197
When i create new User Account and login with that credentials, it gives me this problem if admin doesnt have set thier values yet !!!**  AllotmentList = [select id, Days_Remaining__c from PTO_Allotment__c where Year__c =:year and Employee__c=:EmployeeList[0].id LIMIT 100]; //access only Current user Allotment details                 //to handle Listoutof Bound Exception wrote if-else          if(AllotmentList.size()&gt;0)         {         requests.PTO_Allotment__c=AllotmentList[0].id;         requests.Days_Remaining__c=AllotmentList[0].Days_Remaining__c;         }         else         {             //write a code when AllotmentList is empty ....         }
78889
Is there a way to change all the numeric keys to &quot;Name&quot; without looping through the array (so a php function)? [     0 =&gt; 'blabla',     1 =&gt; 'blabla',     2 =&gt; 'blblll',     // etc ... ]
106755
ok so basically I am asking the question of their name I want this to be one input rather than Forename and Surname.  Now is there any way of splitting this name? and taking just the last word from the "Sentence" e.g.  name = "Thomas Winter" print name.split()    and what would be output is just "Winter"
111873
A password is always combination of characters &amp; numbers. I am so much used to num pad that every time windows start I end up entering wrong password just because NUM lock isn't ON by default. Since it's network login, it takes a bit long time to reset &amp; prompt for right password. Is there a way to enable NUM Lock on when Windows loads?
122930
I'm comparing two list given in the list to see if the item.name exists in both for loop. If not then add into a list to show the error for the missing names.  set&lt;id&gt; setAssetIds = new set&lt;id&gt;(); list&lt;Asset_Store__c&gt;  assets = [select id,name......]; for(Asset_Store__c item : assets ) {     setAssetIds.add(item.id);  }   list&lt;Meter__c&gt;  meters = [select id,name......where id in : setAssetIds];  //now comparing the two lists for(Asset_Store__c a : assets) {    for(Meter__c m : meters)    {      if(a.Name != m.Name)      {        //not matched...        break;      }     } }   the problem with the above approach is that, its not finding the correct name.
62170
How does the code  work with Ctrl + K? I am new to Stack Overflow.   I can't get it with Ctrl + K. I  press space button for 4 times in each line I post.
71067
I was surprised that I didn't find this question already on the site. So, today $ came up after I logged in as a new user. This was unexpected because my main user's prompt starts with username@computername:~$.  So, how do I switch from this other shell to bash?