How can I get the string with more lowercase characters from a given List?









up vote
1
down vote

favorite












How can I get the string with more lowercase characters from a given List<String>?



I've already done it in a functional way, using Java 10 Streams, but I want to do it in an iterative way, using while loops. But I have no idea how to do it.
This is my functional code.



public static Optional<String> stringSearched (List<String> pl) 
return pl.stream()
.max(Comparator.comparing(x->x.chars()
.filter(Character::iLowerCase)
.count()
));










share|improve this question









New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • What have you tried already?
    – Glitcher
    yesterday










  • Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
    – Iván Martín Jiménez
    yesterday














up vote
1
down vote

favorite












How can I get the string with more lowercase characters from a given List<String>?



I've already done it in a functional way, using Java 10 Streams, but I want to do it in an iterative way, using while loops. But I have no idea how to do it.
This is my functional code.



public static Optional<String> stringSearched (List<String> pl) 
return pl.stream()
.max(Comparator.comparing(x->x.chars()
.filter(Character::iLowerCase)
.count()
));










share|improve this question









New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • What have you tried already?
    – Glitcher
    yesterday










  • Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
    – Iván Martín Jiménez
    yesterday












up vote
1
down vote

favorite









up vote
1
down vote

favorite











How can I get the string with more lowercase characters from a given List<String>?



I've already done it in a functional way, using Java 10 Streams, but I want to do it in an iterative way, using while loops. But I have no idea how to do it.
This is my functional code.



public static Optional<String> stringSearched (List<String> pl) 
return pl.stream()
.max(Comparator.comparing(x->x.chars()
.filter(Character::iLowerCase)
.count()
));










share|improve this question









New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











How can I get the string with more lowercase characters from a given List<String>?



I've already done it in a functional way, using Java 10 Streams, but I want to do it in an iterative way, using while loops. But I have no idea how to do it.
This is my functional code.



public static Optional<String> stringSearched (List<String> pl) 
return pl.stream()
.max(Comparator.comparing(x->x.chars()
.filter(Character::iLowerCase)
.count()
));







java while-loop java-stream






share|improve this question









New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Stefan Zobel

2,32021827




2,32021827






New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Iván Martín Jiménez

62




62




New contributor




Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Iván Martín Jiménez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • What have you tried already?
    – Glitcher
    yesterday










  • Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
    – Iván Martín Jiménez
    yesterday
















  • What have you tried already?
    – Glitcher
    yesterday










  • Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
    – Iván Martín Jiménez
    yesterday















What have you tried already?
– Glitcher
yesterday




What have you tried already?
– Glitcher
yesterday












Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
– Iván Martín Jiménez
yesterday




Well, I tried to do it with Lists and foreach loop, and from this, use a while loop. But I am in the Engineering Dregee, so teachers want the exercise just as they do it, and I get stucked. Last year we used it Java 8 Streams and I am very confident with then, but we did not study while loops in depth.
– Iván Martín Jiménez
yesterday












3 Answers
3






active

oldest

votes

















up vote
2
down vote













import java.util.ArrayList;
import java.util.List;

public class Something
public static void main(String args)
ArrayList<String> list = new ArrayList<String>();
list.add("LOSER");
list.add("wInner");
list.add("sECONDpLACE");

System.out.println(findMoreLowerCase(list));


private static String findMoreLowerCase(final List<String> list)
String moreLowerCase = ""; // Tracks the current string with less lowercase
int nrOfLowerCase=0; // Tracks the nr of lowercase for that string

int current; // Used in the for loop
for(String word: list)
current = countLowerCase(word);
if(current > nrOfLowerCase)
// Found a string with more lowercase, update
nrOfLowerCase = current;
moreLowerCase = word;


return moreLowerCase;


private static int countLowerCase(String s)
// Scroll each character and count
int result = 0;
for (char letter : s.toCharArray())
if (Character.isLowerCase(letter))
++result;
;

return result;








share|improve this answer




















  • That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
    – Iván Martín Jiménez
    yesterday










  • Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
    – Tu.ma
    yesterday






  • 1




    No problem! I will try by my own anyways. Like I said, thanks a lot.
    – Iván Martín Jiménez
    yesterday










  • No problem, if you manage to solve please consider marking the answer.
    – Tu.ma
    yesterday

















up vote
0
down vote













The easiest way would to be to iterate over all your words and replace non lower case letters (with a regular expression such as [^a-z]) with empty strings, and then get the length of the string.



The longest string is the one that has the most lower case letters.






share|improve this answer



























    up vote
    0
    down vote













    private String moreLowerCase(final List<String> stringList) 
    String moreLowerCase = "";
    final Iterator<String> iterator = stringList.iterator();

    while (iterator.hasNext())
    final String current = iterator.next().replaceAll("[^a-z]", "");
    if (current.length() > moreLowerCase.length())
    moreLowerCase = current;

    return moreLowerCase;






    share|improve this answer








    New contributor




    Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Iván Martín Jiménez is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224385%2fhow-can-i-get-the-string-with-more-lowercase-characters-from-a-given-list%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      import java.util.ArrayList;
      import java.util.List;

      public class Something
      public static void main(String args)
      ArrayList<String> list = new ArrayList<String>();
      list.add("LOSER");
      list.add("wInner");
      list.add("sECONDpLACE");

      System.out.println(findMoreLowerCase(list));


      private static String findMoreLowerCase(final List<String> list)
      String moreLowerCase = ""; // Tracks the current string with less lowercase
      int nrOfLowerCase=0; // Tracks the nr of lowercase for that string

      int current; // Used in the for loop
      for(String word: list)
      current = countLowerCase(word);
      if(current > nrOfLowerCase)
      // Found a string with more lowercase, update
      nrOfLowerCase = current;
      moreLowerCase = word;


      return moreLowerCase;


      private static int countLowerCase(String s)
      // Scroll each character and count
      int result = 0;
      for (char letter : s.toCharArray())
      if (Character.isLowerCase(letter))
      ++result;
      ;

      return result;








      share|improve this answer




















      • That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
        – Iván Martín Jiménez
        yesterday










      • Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
        – Tu.ma
        yesterday






      • 1




        No problem! I will try by my own anyways. Like I said, thanks a lot.
        – Iván Martín Jiménez
        yesterday










      • No problem, if you manage to solve please consider marking the answer.
        – Tu.ma
        yesterday














      up vote
      2
      down vote













      import java.util.ArrayList;
      import java.util.List;

      public class Something
      public static void main(String args)
      ArrayList<String> list = new ArrayList<String>();
      list.add("LOSER");
      list.add("wInner");
      list.add("sECONDpLACE");

      System.out.println(findMoreLowerCase(list));


      private static String findMoreLowerCase(final List<String> list)
      String moreLowerCase = ""; // Tracks the current string with less lowercase
      int nrOfLowerCase=0; // Tracks the nr of lowercase for that string

      int current; // Used in the for loop
      for(String word: list)
      current = countLowerCase(word);
      if(current > nrOfLowerCase)
      // Found a string with more lowercase, update
      nrOfLowerCase = current;
      moreLowerCase = word;


      return moreLowerCase;


      private static int countLowerCase(String s)
      // Scroll each character and count
      int result = 0;
      for (char letter : s.toCharArray())
      if (Character.isLowerCase(letter))
      ++result;
      ;

      return result;








      share|improve this answer




















      • That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
        – Iván Martín Jiménez
        yesterday










      • Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
        – Tu.ma
        yesterday






      • 1




        No problem! I will try by my own anyways. Like I said, thanks a lot.
        – Iván Martín Jiménez
        yesterday










      • No problem, if you manage to solve please consider marking the answer.
        – Tu.ma
        yesterday












      up vote
      2
      down vote










      up vote
      2
      down vote









      import java.util.ArrayList;
      import java.util.List;

      public class Something
      public static void main(String args)
      ArrayList<String> list = new ArrayList<String>();
      list.add("LOSER");
      list.add("wInner");
      list.add("sECONDpLACE");

      System.out.println(findMoreLowerCase(list));


      private static String findMoreLowerCase(final List<String> list)
      String moreLowerCase = ""; // Tracks the current string with less lowercase
      int nrOfLowerCase=0; // Tracks the nr of lowercase for that string

      int current; // Used in the for loop
      for(String word: list)
      current = countLowerCase(word);
      if(current > nrOfLowerCase)
      // Found a string with more lowercase, update
      nrOfLowerCase = current;
      moreLowerCase = word;


      return moreLowerCase;


      private static int countLowerCase(String s)
      // Scroll each character and count
      int result = 0;
      for (char letter : s.toCharArray())
      if (Character.isLowerCase(letter))
      ++result;
      ;

      return result;








      share|improve this answer












      import java.util.ArrayList;
      import java.util.List;

      public class Something
      public static void main(String args)
      ArrayList<String> list = new ArrayList<String>();
      list.add("LOSER");
      list.add("wInner");
      list.add("sECONDpLACE");

      System.out.println(findMoreLowerCase(list));


      private static String findMoreLowerCase(final List<String> list)
      String moreLowerCase = ""; // Tracks the current string with less lowercase
      int nrOfLowerCase=0; // Tracks the nr of lowercase for that string

      int current; // Used in the for loop
      for(String word: list)
      current = countLowerCase(word);
      if(current > nrOfLowerCase)
      // Found a string with more lowercase, update
      nrOfLowerCase = current;
      moreLowerCase = word;


      return moreLowerCase;


      private static int countLowerCase(String s)
      // Scroll each character and count
      int result = 0;
      for (char letter : s.toCharArray())
      if (Character.isLowerCase(letter))
      ++result;
      ;

      return result;









      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered yesterday









      Tu.ma

      358111




      358111











      • That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
        – Iván Martín Jiménez
        yesterday










      • Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
        – Tu.ma
        yesterday






      • 1




        No problem! I will try by my own anyways. Like I said, thanks a lot.
        – Iván Martín Jiménez
        yesterday










      • No problem, if you manage to solve please consider marking the answer.
        – Tu.ma
        yesterday
















      • That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
        – Iván Martín Jiménez
        yesterday










      • Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
        – Tu.ma
        yesterday






      • 1




        No problem! I will try by my own anyways. Like I said, thanks a lot.
        – Iván Martín Jiménez
        yesterday










      • No problem, if you manage to solve please consider marking the answer.
        – Tu.ma
        yesterday















      That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
      – Iván Martín Jiménez
      yesterday




      That way is very intuitive, but I need to code it with while loops. I´ll see if I can do it from this code. Thank you so much, very helpful :)
      – Iván Martín Jiménez
      yesterday












      Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
      – Tu.ma
      yesterday




      Sorry, I just read "loops". Anyway, it should be trivial moving from for loops to while loops, and it can help you understand it better. Let me know if you have problems.
      – Tu.ma
      yesterday




      1




      1




      No problem! I will try by my own anyways. Like I said, thanks a lot.
      – Iván Martín Jiménez
      yesterday




      No problem! I will try by my own anyways. Like I said, thanks a lot.
      – Iván Martín Jiménez
      yesterday












      No problem, if you manage to solve please consider marking the answer.
      – Tu.ma
      yesterday




      No problem, if you manage to solve please consider marking the answer.
      – Tu.ma
      yesterday












      up vote
      0
      down vote













      The easiest way would to be to iterate over all your words and replace non lower case letters (with a regular expression such as [^a-z]) with empty strings, and then get the length of the string.



      The longest string is the one that has the most lower case letters.






      share|improve this answer
























        up vote
        0
        down vote













        The easiest way would to be to iterate over all your words and replace non lower case letters (with a regular expression such as [^a-z]) with empty strings, and then get the length of the string.



        The longest string is the one that has the most lower case letters.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          The easiest way would to be to iterate over all your words and replace non lower case letters (with a regular expression such as [^a-z]) with empty strings, and then get the length of the string.



          The longest string is the one that has the most lower case letters.






          share|improve this answer












          The easiest way would to be to iterate over all your words and replace non lower case letters (with a regular expression such as [^a-z]) with empty strings, and then get the length of the string.



          The longest string is the one that has the most lower case letters.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          npinti

          45.8k55481




          45.8k55481




















              up vote
              0
              down vote













              private String moreLowerCase(final List<String> stringList) 
              String moreLowerCase = "";
              final Iterator<String> iterator = stringList.iterator();

              while (iterator.hasNext())
              final String current = iterator.next().replaceAll("[^a-z]", "");
              if (current.length() > moreLowerCase.length())
              moreLowerCase = current;

              return moreLowerCase;






              share|improve this answer








              New contributor




              Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                0
                down vote













                private String moreLowerCase(final List<String> stringList) 
                String moreLowerCase = "";
                final Iterator<String> iterator = stringList.iterator();

                while (iterator.hasNext())
                final String current = iterator.next().replaceAll("[^a-z]", "");
                if (current.length() > moreLowerCase.length())
                moreLowerCase = current;

                return moreLowerCase;






                share|improve this answer








                New contributor




                Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  private String moreLowerCase(final List<String> stringList) 
                  String moreLowerCase = "";
                  final Iterator<String> iterator = stringList.iterator();

                  while (iterator.hasNext())
                  final String current = iterator.next().replaceAll("[^a-z]", "");
                  if (current.length() > moreLowerCase.length())
                  moreLowerCase = current;

                  return moreLowerCase;






                  share|improve this answer








                  New contributor




                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  private String moreLowerCase(final List<String> stringList) 
                  String moreLowerCase = "";
                  final Iterator<String> iterator = stringList.iterator();

                  while (iterator.hasNext())
                  final String current = iterator.next().replaceAll("[^a-z]", "");
                  if (current.length() > moreLowerCase.length())
                  moreLowerCase = current;

                  return moreLowerCase;







                  share|improve this answer








                  New contributor




                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered yesterday









                  Volodymyr Strontsitskyi

                  1




                  1




                  New contributor




                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Volodymyr Strontsitskyi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.




















                      Iván Martín Jiménez is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      Iván Martín Jiménez is a new contributor. Be nice, and check out our Code of Conduct.












                      Iván Martín Jiménez is a new contributor. Be nice, and check out our Code of Conduct.











                      Iván Martín Jiménez is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224385%2fhow-can-i-get-the-string-with-more-lowercase-characters-from-a-given-list%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo