#If statement giving opposite result

1 messages · Page 1 of 1 (latest)

lapis apex
atomic grailBOT
#

<@&987246399047479336> please have a look, thanks.

atomic grailBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

safe coral
#

@lapis apex i think you misunderstood what i meant by if you wanna store one element. My point was the length is decided by the number of elements you wanna store in your array. Think of array just like train boggies, where each boggy holds a references to a certain value. Do you wanna store 5 values? go for that length. If you only wanna store a single value then a simple variable of type int would be sufficient. int sixDig = valueHere. Arrays are just more convenient way to store multiple values.

#
//this looks painful if you a lot of values
int numberOne = 1;
int numberTwo = 2;
int numberThree =3;
int numberFour = 4;
int numberFive =5;
int numberSix = 6;

//this is how you would do it with arrays
int [] values = {1,2,3,4,5,6.........};
lapis apex
#

i think i got ur point but

safe coral
#

now you can just iterate over values using indexing and get all values

lapis apex
#

it says " Your program will then generate a sixdigits random number that will be sent to the output to be received by the customer.
Your program should save generated account numbers in an array, so that each
generated number is compared against the stored numbers to avoid generating
duplicate account numbers."

#

now that i've read it again i think i shouldn't use the array to generate the random number but to store the generated accounts by the customer

safe coral
#

they want you to have an array which will store all the account numbers, so that you can check all the values and not make a duplicate account number. Since two person with same account number would be really bad.

lapis apex
#

can u give me an idea how could it be?

safe coral
#

you generate a new number, iterate over already added account numbers array and if it's duplicate then generate again and if it's unique add it to that array.

#

Did you close the earlier thread? or is it still up?

lapis apex
#

i closed the previous one

atomic grailBOT
#

@lapis apex

TL;DR: Comparing some types with ==, (especially String references) can have confusing results. Always use a.equals(b) for Strings.

A variable's type is either a primitive or a reference:

  • Primitive values are, for example, 0 or 1 for int, true or false for boolean, 'a' or '*' for char...
  • Reference values are either null or a reference to an object. eg The value of s in String s = "Foo" is reference to a String object, not the object itself.

Using == compares the values of two variables. If two reference values are the same, they refer to the same object or are both null.

Using a.equals(b) calls a method on a that compares its contents to the object referenced by b. You are asking if the two objects are alike.

Imagine you know the following three people:

  • jane
  • bob
  • Bob's identical twin brother michael, also known as mike.

The following are examples of using == and .equals:

  • jane == bob - false. They are not the same person
  • jane.equals(bob) - false. They are not alike
  • bob == michael - false. They are not the same person
  • bob.equals(michael) - true. They are alike
  • michael == mike - true. They are the same person

Strings are special in Java and, because of this, comparing references to Strings can be surprising. Though there are rare cases where it is useful and necessary to use == with String references, you should almost always be using .equals(Object) to compare the Strings of two references. For two strings a and b use a.eqauls(b) rather than a == b.

Note: a.equals(b) is intended to see if the two objects referenced by a and b are alike. How alike they must be depends on the implementation of equals for the class of a. The default implementation of equals, for a class extending Object, has the same behaviour as using ==.