aspose file tools*
The moose likes Beginning Java and the fly likes Program condition Cha Java does not work for five elements inserted in the List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program condition Cha Java does not work for five elements inserted in the List" Watch "Program condition Cha Java does not work for five elements inserted in the List" New topic
Author

Program condition Cha Java does not work for five elements inserted in the List

Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
I'm trying to run this code, but the routine that I put to you to enter five elements in the ArrayList, and simply does not enter the 6th element, because it does not work, below is a Routine






Chat program to run, provided you insert 5 elements in the list, because it does not work?

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

marcio duran wrote:
I'm trying to run this code, but the routine that I put to you to enter five elements in the ArrayList, and simply does not enter the 6th element, because it does not work, below is a Routine



[HENRY: Formatting modified]


Agreed. A sixth element is never added. From your code, the code that conditional add elements, is in a condition that is only triggered if there are less than 5 elements. Since the code can never be triggered when there are five or more elements, obviously, it can never be six elements.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Henry Wong wrote:
marcio duran wrote:
I'm trying to run this code, but the routine that I put to you to enter five elements in the ArrayList, and simply does not enter the 6th element, because it does not work, below is a Routine



[HENRY: Formatting modified]


Agreed. A sixth element is never added. From your code, the code that conditional add elements, is in a condition that is only triggered if there are less than 5 elements. Since the code can never be triggered when there are five or more elements, obviously, it can never be six elements.

Henry
When you start adding elements, it must hold in this case it will add the list until you reach the maximum of 5 elements, then how do you code this condition that could illustrate for this condition?

/* public boolean armazena(String newName){

for (int i=0;LISTA_DE_NOMES.size()<=5; i++){
if(LISTA_DE_NOMES.get(i).equals(newName))
return true;
}


LISTA_DE_NOMES.add(newName);
return false;
}*/

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
The way you are handling that code, you are not using the List as a List. You are actually using it as a Set, because you are not permitting duplicates. Do you require a Set which does not support duplicates, in combination with recording the order of insertion. If so, go through the Java Tutorials. You will find all sorts of implementations, including one which combines a linked list and a Set. That might provide what you want all in one class.
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Campbell Ritchie wrote:The way you are handling that code, you are not using the List as a List. You are actually using it as a Set, because you are not permitting duplicates. Do you require a Set which does not support duplicates, in combination with recording the order of insertion. If so, go through the Java Tutorials. You will find all sorts of implementations, including one which combines a linked list and a Set. That might provide what you want all in one class.


When you are filling the ArrayList, use a condition for each element. For example:



Thank you !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
If you are only adding new elements to the List, you do not have a List, but an ordered Set. I suggest you consider one of the implementations of java.util.Set which records insertion order instead of a List.

There aren’t very many to choose from, unless you write your own
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Campbell Ritchie wrote:If you are only adding new elements to the List, you do not have a List, but an ordered Set. I suggest you consider one of the implementations of java.util.Set which records insertion order instead of a List.

There aren’t very many to choose from, unless you write your own


Muito Obrigado, pela sua atenção você esta me ajudando bastante !!!

[google translation -- Thank you, for your attention you are helping me a lot!]

Será que seria perto disso , bom eu estou pesquisando !!! Set<String> setObject = new HashSet<String>(listObject);

[google translation -- Would it be close, well I'm researching]

Thank You !!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

marcio duran wrote:Será que seria perto disso , bom eu estou pesquisando !!! Set<String> setObject = new HashSet<String>(listObject);

Sounds like a good start to me, but there's no experience like doing.

Try it out, and see if you run into problems.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Winston Gutkowski wrote:
marcio duran wrote:Será que seria perto disso , bom eu estou pesquisando !!! Set<String> setObject = new HashSet<String>(listObject);

Sounds like a good start to me, but there's no experience like doing.

Try it out, and see if you run into problems.

Winston


"Friends, I'm seeing a lot of codes and some examples in some implementations it seems to me that imagination comes to amazing algorithms format in Java language, but it is necessary to share global experiences and this is called science"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
We speak English on this website!

As Winston said, by doing.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

marcio duran wrote:"Friends, I'm seeing a lot of codes and some examples in some implementations it seems to me that imagination comes to amazing algorithms format in Java language, but it is necessary to share global experiences and this is called science"

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.

Winston
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Campbell Ritchie wrote:We speak English on this website!

As Winston said, by doing.


We speak Java on this website !
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Winston Gutkowski wrote:
marcio duran wrote:"Friends, I'm seeing a lot of codes and some examples in some implementations it seems to me that imagination comes to amazing algorithms format in Java language, but it is necessary to share global experiences and this is called science"

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.

Winston


Thank you !
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

marcio duran wrote:
Campbell Ritchie wrote:We speak English on this website!

As Winston said, by doing.


We speak Java on this website !


Agreed. However, JavaRanch is a moderated site, and unfortunately, we do not have enough moderators for all the forums (if we have to deal with every language). Hence, we do require that all non-code be posted in English.

Thank you for your understanding,
Henry
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Henry Wong wrote:
marcio duran wrote:
Campbell Ritchie wrote:We speak English on this website!

As Winston said, by doing.


We speak Java on this website !


Agreed. However, JavaRanch is a moderated site, and unfortunately, we do not have enough moderators for all the forums (if we have to deal with every language). Hence, we do require that all non-code be posted in English.

Thank you for your understanding,
Henry


Hello Henry!

I agree with you but I mean I have my opinion about what we are guiding lines we can also illustrate by an example of code, I'm not complaining about anything everyone here have room to do their placements, but employees can be more involved on languages ​​or who are here concerned to produce a common intelligence I mean that my interest is Java, there is no other intentions or protests.

Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I don’t understand the last post.
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Campbell Ritchie wrote:I don’t understand the last post.


"Writing in English often do not help much when I'm thinking in Portuguese, sorry"
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Marcio Duran wrote:"Writing in English often do not help much when I'm thinking in Portuguese, sorry"

Then I fear you'll have to find a Portugese site.

Also: please don't write in all bold text. It comes across as "shouting".

Winston
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Winston Gutkowski wrote:
Marcio Duran wrote:"Writing in English often do not help much when I'm thinking in Portuguese, sorry"

Then I fear you'll have to find a Portugese site.

Also: please don't write in all bold text. It comes across as "shouting".

Winston


The issue is Java, not philosophy if you want to work required, if not just ignore me, I will not get upset about it or ask you to look for a site that is not in my best interest
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21

I think it should come, what I'm wondering

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Marcio Duran wrote:I think it should come, what I'm wondering

What are you wondering? We still don't know yet.

However, a few points about your code:

1. Don't use ALL_CAPS for field names: they should start with a lower-case letter. See here or here for a list of the main naming conventions.

2. Don't simply throw Exception. At the very least you should throw a RuntimeException, because that's what it is - you also don't have to declare it in the method signature - but better still is to throw an Exception of your own that describes the error (RoomSizeExceededException?).

3. Your code is brittle because you've hard-wired the maximum room size. What about supplying it at construction time?:then you can set up a room with:
new Sala(5);

and check whether you've exceeded its limit with:
if(listaDeNomes.size() >= listaDeNomes.getMaxSize()) {
   throw new RoomSizeExceededException("Esta sala já esta cheia");
}
...

Better still: put ALL that logic in Sala itself; perhaps a novoUsario(String) method.

HIH

Winston
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Winston Gutkowski wrote:
Marcio Duran wrote:I think it should come, what I'm wondering

What are you wondering? We still don't know yet.

However, a few points about your code:

1. Don't use ALL_CAPS for field names: they should start with a lower-case letter. See here or here for a list of the main naming conventions.

2. Don't simply throw Exception. At the very least you should throw a RuntimeException, because that's what it is - you also don't have to declare it in the method signature - but better still is to throw an Exception of your own that describes the error (RoomSizeExceededException?).

3. Your code is brittle because you've hard-wired the maximum room size. What about supplying it at construction time?:then you can set up a room with:
new Sala(5);

and check whether you've exceeded its limit with:
if(listaDeNomes.size() >= listaDeNomes.getMaxSize()) {
   throw new RoomSizeExceededException("Esta sala já esta cheia");
}
...

Better still: put ALL that logic in Sala itself; perhaps a novoUsario(String) method.

HIH

Winston



Thank You !!!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Marcio Duran wrote:Thank You !!!

Don't thank me yet:
1. java.util.Set is an interface, NOT a class; so you can implement it any way you like.
2. Furthermore: most Java classes that do implement it (eg, java.util.HashSet) are NOT final; therefore you can subclass them.
3. All that "is it OK to add this user" logic (including your armazena() and canAddNewUser() methods) can therefore (and should) be put inside your Sala class.

Given that, show us what your Sala class should look like.

Winston
Marcio Duran
Greenhorn

Joined: Apr 23, 2012
Posts: 21
Winston Gutkowski wrote:
Marcio Duran wrote:Thank You !!!

Don't thank me yet:
1. java.util.Set is an interface, NOT a class; so you can implement it any way you like.
2. Furthermore: most Java classes that do implement it (eg, java.util.HashSet) are NOT final; therefore you can subclass them.
3. All that "is it OK to add this user" logic (including your armazena() and canAddNewUser() methods) can therefore (and should) be put inside your Sala class.

Given that, show us what your Sala class should look like.

Winston


Thank You , Winston !!!

I want to ask It's valid!

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Marcio Duran wrote:I want to ask It's valid!

Fraid not:
  • What if nomeCliente.size() is > 5?
  • What if adding all the names in nomeCliente would make LISTA_DE_NOMES.size() > 5?

  • Keep plugging away. You'll get there.

    Winston
    Marcio Duran
    Greenhorn

    Joined: Apr 23, 2012
    Posts: 21
    Winston Gutkowski wrote:
    Marcio Duran wrote:I want to ask It's valid!

    Fraid not:
  • What if nomeCliente.size() is > 5?
  • What if adding all the names in nomeCliente would make LISTA_DE_NOMES.size() > 5?

  • Keep plugging away. You'll get there.

    Winston


    The One is to refurbish this project, which is poorly structured and misconceptions still using object-oriented, I have to separate the responsibilities for which is intended to control access via a socket and is designed to validate clients that go into the room .

    Thank You!!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38363
        
      23
    Finish the validation here before you connect to a socket.
    Marcio Duran
    Greenhorn

    Joined: Apr 23, 2012
    Posts: 21
    Campbell Ritchie wrote:Finish the validation here before you connect to a socket.


    I'm thinking the designer code, programming was in second place
    Marcio Duran
    Greenhorn

    Joined: Apr 23, 2012
    Posts: 21
    Marcio Duran wrote:
    Campbell Ritchie wrote:Finish the validation here before you connect to a socket.


    I'm thinking the designer code, programming was in second place

    other utilities

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Program condition Cha Java does not work for five elements inserted in the List