Forums Register Login

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

+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 !
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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 !!!
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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"
+Pie Number of slices to send: Send
We speak English on this website!

As Winston said, by doing.
1
+Pie Number of slices to send: Send
 

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
1
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:We speak English on this website!

As Winston said, by doing.



We speak Java on this website !
+Pie Number of slices to send: Send
 

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 !
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
I don’t understand the last post.
+Pie Number of slices to send: Send
 

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"
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send

I think it should come, what I'm wondering

+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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 !!!
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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!

+Pie Number of slices to send: Send
 

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
    +Pie Number of slices to send: Send
     

    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!!
    +Pie Number of slices to send: Send
    Finish the validation here before you connect to a socket.
    +Pie Number of slices to send: Send
     

    Campbell Ritchie wrote:Finish the validation here before you connect to a socket.



    I'm thinking the designer code, programming was in second place
    +Pie Number of slices to send: Send
     

    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

    Let's get him boys! We'll make him read this tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 1729 times.
    Similar Threads
    Issue with WeblogicServer
    Problema com servi̤o do Tomcat ap�?s um periodo de inativid
    use Linux sendmail from Java
    �Rich Client + EJB�
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 07:27:52.