• Post Reply Bookmark Topic Watch Topic
  • New Topic

variables in java  RSS feed

 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I assign multiple values to one variable?
If so can someone show an example of it?
for example I want myNum = 0 thru 9
you see im trying to program a password checker program to verify that the password meets all the criteria 8 char long, 1 upper case, 1 lower case, 1 numeric, 1 special, and don't contain and or end
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how exactly would you expect that to work? Forget about java...what would it mean in English?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you looking for something like a set of elements? A container to which you can put any (of fixed?) number of elements?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do that you will need to make use of an array or a collection data structure such as a set, list, queue or map.

Arrays store values of the same type and are fixed in size (how much values the variable will store) at the point of declaration and initialization;

For primitive values its as follows:



For object values its as follows:



You can also use a loop:



If you want a dynamically growing variable of values you will have to use a data structure that allow this functionality which can be found in the java.util package... These are structures such as sets, lists, queues and maps.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that in every case listed above, the variable holds one value - the reference to the collection. Sure, those collections can hold many values, but the VARIABLE holds one thing, and one thing only - the means to get to all the values.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I would argue that in every case listed above, the variable holds one value - the reference to the collection. Sure, those collections can hold many values, but the VARIABLE holds one thing, and one thing only - the means to get to all the values.


Thanks for expanding and clarifying my attempted explanation, this is correct. The array itself is an object and the collections (data structures) are also objects so the variable will be a reference to the object storing the values
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote:Can I assign multiple values to one variable?
If so can someone show an example of it?
for example I want myNum = 0 thru 9
you see im trying to program a password checker program to verify that the password meets all the criteria 8 char long, 1 upper case, 1 lower case, 1 numeric, 1 special, and don't contain and or end

here is what I got so far on the password checker program
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to compile it?

1st problem. Lines 10 and 19. You try to declare variable of type arrayList. Remember Java is case sensitive.
2nd problem. String does not have method lenght.
3rd problem. You are trying to use raw array list. Specify a type. In your case like this: ArrayList<Character>.
4th problem. Lines 40 and 45. You try to pass ArrayList into String#indexOf. That method takes char or String as an argument.
5th problem. Lines 40 and 45. You try to compare result of String#indexOf to boolean type. That's illegal as that method returns int, not boolean.
6th problem. Don't write:
  • if(condition == true) or
  • if(condition != true)
  • Use
  • if(condition) or
  • if(!condition)
  • 7th problem. You are cramming everything inside main method. Don't do that. And read this: MainIsAPain

    It looks like you wrote bunch of code without even compiling it. Don't do that. Write a couple of lines, compile, test if it works, correct mistakes, repeat.
    And maybe you should read this: StopCoding
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you've written about ten times the code you should before you compile. Personally, i would never write more than three lines before trying to compile and test it all.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!