Reset Boolean values? - boolean

I am trying to get my password verifier to loop until a correct response is give. It's looping right now, but its not resetting the boolean values after the first iteration, resulting in a valid response the second time no matter which password. How do I make the loop check for every value, or reset the table every time?
boolean atLeast8 = false;
boolean oneLower = false;
boolean oneUpper = false;
boolean oneNumber = false;
boolean oneSpecial = false;
boolean noAnd = false;
boolean noEnd = false;
do
{
System.out.println("Enter your password. " + '\n');
password = input.nextLine();
System.out.println();
for(int i=0; i<password.length(); i++) //Checks for values througout the length of the string
{
char c = password.charAt(i);
if(password.length() >= 8)
atLeast8 = true;
if(Character.isLowerCase(c))
oneLower = true;
if(Character.isUpperCase(c))
oneUpper = true;
if(Character.isDigit(c))
oneNumber = true;
if(c=='!' || c=='#' || c=='#' || c=='$' || c=='%' || c=='^' || c=='&' || c=='&' || c=='*')
oneSpecial = true;
if (password.indexOf("and") < 0)
noAnd = true;
if (password.indexOf("end") < 0)
noEnd = true;
}
if(atLeast8 && oneLower && oneUpper && oneNumber && oneSpecial && noAnd && noEnd) //Does the string contain any true values?
{
System.out.println("Valid.");
}
else
{
System.out.println("Invalid!"); //Does the string contain any false values?
}
if(!atLeast8)
{
System.out.println("Must be at least 8 characters long.");
}
if(!oneLower)
{
System.out.println("Must contain one lower case letter.");
}
if(!oneUpper)
{
System.out.println("Must contain one upper case letter.");
}
if(!oneNumber)
{
System.out.println("Must contain one numeric digit.");
}
if(!oneSpecial)
{
System.out.println("Must contain one special character.");
}
if(!noAnd)
{
System.out.println("Must not contain the word 'and'.");
}
if(!noEnd)
{
System.out.println("Must not contain the word 'end'.");
}
}while (atLeast8 == false || oneLower == false || oneUpper == false || oneNumber == false || oneSpecial == false || noAnd == false || noEnd == false);
}
}

Can't you just put
atLeast8 = false;
oneLower = false;
oneUpper = false;
oneNumber = false;
oneSpecial = false;
noAnd = false;
noEnd = false;
at the start of the loop (somewhere between the "do {" and the "for")
Or, is that being too simplistic?

Related

why the filter does not work as expected?

how can I make the logic so when I press vegan and lactose it will show me a meal that contains at least one of them or both?
in the current state it shows only meals that contains both.
Map<String, bool> _filters = {
'gluten': false,
'lactose': false,
'vegan': false,
'vegetarian': false,
};
//above is a switchTile so the values change
_availableMeals = DUMMY_MEALS.where((meal) {
if (_filters['gluten']! && !meal.isGlutenFree) {
return false;
}
if (_filters['lactose']! && !meal.isLactoseFree) {
return false;
}
if (_filters['vegan']! && !meal.isVegan) {
return false;
}
if (_filters['vegetarian']! && !meal.isVegetarian) {
return false;
}
return true;
}).toList();
Here you go. Part of the problem was that you were using ! to mean == false
_availableMeals = DUMMY_MEALS.where((meal) {
// actually i'm not sure about this part, try removing it if it doesn't make sense in your UI
if (_filters['gluten'] == false &&
_filters['lactose'] == false &&
_filters['vegan'] == false &&
_filters['vegatarian'] == false) {
return true;
}
if (_filters['gluten'] == true && meal.isGlutenFree) {
return true;
}
if (_filters['lactose'] == true && meal.isLactoseFree) {
return true;
}
if (_filters['vegan'] == true && meal.isVegan) {
return true;
}
if (_filters['vegetarian'] == true && meal.isVegetarian) {
return true;
}
return false;
}).toList();

How to check a group of text edit controllers to see if any of them are null?

I'd like to have a function that checks to see if any of the text edit controller fields are null.
I wrote this method below, but I wanted to see if there was a more elegant solution.
bool _nullfieldExists(){
//return a true if any of the textEditController fields are null
if (_textEditControllerNumberPlayers.text == null ){
return true;
}
else if (_textEditControllerSmallBlind.text == null ){
return true;
}
else if (_textEditControllerBigBlind.text == null ){
return true;
}
else if (_textEditControllerAnte.text == null ){
return true;
}
else if (_textEditControllerMyStack.text == null ){
return true;
}
else {
return false;
}
}

How to insert an Element in a doubly linked list without repetion?

I am trying to write a code that inserts an Element in a doubly linked list without repetition but it's not working.
Any help would be appreciated.
This is my code:
public void insert(int value) {
Element tmp = new Element(value);
if(this.head == null) {
this.head = this.rear = tmp;
return ;
}
if(this.head.data < value) {
tmp.next = this.head;
this.head.previous = tmp;
tmp.previous = null;
this.head = tmp;
return ;
}
if(this.rear.data > value) {
tmp.previous = this.rear;
this.rear.next = tmp;
this.rear = tmp;
return ;
}
else {
Element cur = this.head;
while(cur.next != null && cur.next.data > value)
cur = cur.next;
tmp.next = cur.next;
tmp.previous = cur;
cur.next.previous = tmp;
cur.next = tmp;
}
return ;
}
You should add a second method:
public boolean isInList(int value) {
Element cur = this.head;
if(this.head == null)
return false;
while(cur != null) {
if(cur.data == value)
return true;
cur = cur.next;
}
return false;
}
And then add it to the insert method:
public void insert(int value) {
Element tmp = new Element(value);
if(this.isInList(value))
return false;
if(this.head == null) {
this.head = this.rear = tmp;
return ;
}
if(this.head.data < value) {
tmp.next = this.head;
this.head.previous = tmp;
tmp.previous = null;
this.head = tmp;
return ;
}
if(this.rear.data > value) {
tmp.previous = this.rear;
this.rear.next = tmp;
this.rear = tmp;
return ;
}
else {
Element cur = this.head;
while(cur.next != null && cur.next.data > value)
cur = cur.next;
tmp.next = cur.next;
tmp.previous = cur;
cur.next.previous = tmp;
cur.next = tmp;
}
return ;
}

Passing parameters for methods in Scala (alternatives to change variables values)

I need translate a Java code to Scala, but the compiler show me error. I understand that parameter input on methods are val type. Which alternative i can adopt if i need transform these values? I think to apply case class or class... Below the snippet code (in Scala):
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
return null
}else if(x == null && y == null){
return this
}else if(this.root == null && x == null){
return y
}else if(this.root == null && y == null){
return x
}else if(x == null){
y = y.pmerge(this) //error
null
}else if(this.root == null){
y = y.pmerge(x) //error
null
}else if (y == null){
y = this.pmerge(x) // error
null
}else{
y = y.pmerge(x)
this
}
}
Note that error is showed where y parameter is updated.
Thanks
Yes, the error is shown because you cannot reassign something to val, and parameters to methods in Scala are only sent as vals (immutables).
Because you don't provide the full definition of this, it's difficult to suggest an alternative solution, but:
In general, instead of if-else "Java" style, in Scala you can use pattern matching, and instead of null you can use Option, which is very powerful.
For Example, I suggest refactoring your method in this "Scala" Style (partial implementation)
def pmerge_FA(x: Pennant, y: Pennant): Option[Pennant] = {
(Option(x),Option(y), Option(this.root)) match {
case (None, None, None) => None
case (None, None, _) => Option("")
case (None, _, None) => Option(y)
case (_, None, None) => Option(x)
case (None, _, _) =>
....
}
}
Such that you will return the x, y as their new values, or create a case class like:
case class PennantCaseClass (x:Pennant, y:Pennant)
And returning it when needed.
Again, If you will provide some more info about Pennant class it will be easier to give a better alternative implementation for this method.
New values of y (namely y.pmerge(...)) are never used after assignments. So I guess all assignments y = y.pmerge(...) can be replaced with just invocations y.pmerge(...).
Does y.pmerge(...) do any side effects? Just in case, if not then values y.pmerge(...) are never used (only null or this is returned), so in such case lines y = y.pmerge(...) can be removed at all.
So the code can be either (if there are side effects)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
y.pmerge(this)
null
}else if(this.root == null){
y.pmerge(x)
null
}else if (y == null){
this.pmerge(x)
null
}else{
y.pmerge(x)
this
}
}
or (if there are no side effects)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
null
}else if(this.root == null){
null
}else if (y == null){
null
}else{
this
}
}
Oh, right! There are three classes to build a Bag data structure object, with nodes added in perfectly balanced tree. These methods works in that one. Below the complete code (in Java). The Pennant class build a forest using the node of graph object.
Node Class:
public class Node {
private Node left;
private Node right;
private int item;
public Node() {
left = null;
right = null;
item = 0;
}
public Node(int value) {
left = null;
right = null;
item = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public int getItem() {
return this.item;
}
public void setItem(int item) {
this.item = item;
}
}
public class Pennant{
private Node root;
public Pennant() {
this.root = null;
}
public Pennant(int value) {
this.root = new Node(value);
}
public void setRoot(Node root) {
this.root = root;
}
public Node getRoot() {
return this.root;
}
public Pennant pmerge(Pennant y) {
if(this.getRoot() == null) {
return y;
}else {
this.getRoot().setRight(y.getRoot().getLeft());
y.getRoot().setLeft(this.getRoot());
}
return y;
}
public Pennant pmerge_FA(Pennant x, Pennant y) {
if(x == null && y == null && this.getRoot() == null) {
return null;
}else if(x == null && y == null) {
return this;
}else if(this.getRoot() == null && x == null) {
return y;
}else if(this.getRoot() == null && y == null) {
return x;
}else if(x == null) {
y = y.pmerge(this);
return null;
}else if(this.getRoot() == null) {
y = y.pmerge(x);
return null;
}else if (y == null) {
y = this.pmerge(x);
return null;
}else {
y = y.pmerge(x);
return this;
}
}
public Pennant psplit() {
if(this.getRoot() != null && this.getRoot().getLeft() != null) {
Pennant y = new Pennant();
y.setRoot(this.getRoot().getLeft());
this.getRoot().setLeft(y.getRoot().getRight());
y.getRoot().setRight(null);
return y;
}
return null;
}
public void remove_all(Node node) {
if (node.getLeft() != null) {
remove_all(node.getLeft());
}
if(node.getRight() != null) {
remove_all(node.getRight());
}
node = null;
}
}

STM32 atoi and strtol sometimes missing first 2 digits

I am reading a value sent over RS485 which is the value of an encoder I first check if it has returned an E character (the encoder is reporting an error) and if not then do the following
*position = atoi( buffer );
// Also tried *position = (s32) strtol(buffer,NULL,10);
The value in the buffer is 4033536 and position gets set to 33536 this does not happen every time in this function probably 1 in 1000 times maybe although I am not counting. Setting the program counter back and doing the line again if has failed returns the same result but starting the debugger again causes the value to convert correctly.
I am using keil uvision 4, its a custom board using an stm32f103vet6 and the stm32f10 library V2.0.1 This one has really got me stumped never come across something like this before any help would be much appreciated.
Thanks
As no one knows I will just post what I ended up doing which was to write my own function for convertion not ideal but it worked.
bool cdec2s32(char* text, s32 *destination)
{
s32 tempResult = 0;
char currentChar;
u8 numDigits = 0;
bool negative = FALSE;
bool warning = FALSE;
if(*text == '-')
{
negative = TRUE;
text++;
}
while(*text != 0x00 && *text != '\r') //while current character not null or carridge return
{
numDigits++;
if(*text >= '0' && *text <= '9')
{
currentChar = *text;
currentChar -= '0';
if((warning && ((currentChar > 7 && !negative) || currentChar > 8 && negative )) || numDigits > 10) // Check number not too large
{
tempResult = 2147483647;
if(negative)
tempResult *= -1;
*destination = tempResult;
return FALSE;
}
tempResult *= 10;
tempResult += currentChar;
text++;
if(numDigits >= 9)
{
if(tempResult >= 214748364)
{
warning = TRUE; //Need to check next digit as close to limit
}
}
}
else if(*text == '.' || *text == ',')
{
break;
}
else
return FALSE;
}
if(negative)
tempResult *= -1;
*destination = tempResult;
return TRUE;
}