• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marcio Duran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
}*/


 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marcio Duran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We speak English on this website!

As Winston said, by doing.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:We speak English on this website!

As Winston said, by doing.



We speak Java on this website !
 
Marcio Duran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don’t understand the last post.
 
Marcio Duran
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think it should come, what I'm wondering

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Finish the validation here before you connect to a socket.
     
    Marcio Duran
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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

     
    reply
      Bookmark Topic Watch Topic
    • New Topic