Find Two largest numbers in a list without using ArrayHow can I concatenate two arrays in Java?How do I join two lists in Java?Converting array to list in JavaWriting two dimensional array in javaHow can I “delimit” an integer from a given string?Java error in calculation when comparing largest and smallest numbers & sum with while loopEndless loop created if scanner receives something other than an intEntering (input/scanner) two same numbers consecutively to break a loop “while” Java

Can I submit a paper under an alias so as to avoid trouble in my country?

How to decide whether an eshop is safe or compromised

Are there categories whose internal hom is somewhat 'exotic'?

Using は before 欲しい instead が

How do slats reduce stall speed?

What is "super" in superphosphate?

Find Two largest numbers in a list without using Array

TechSupport Issue ID#812

Has any ancient or medieval Vedic scholar refuted the guna-based varna theory?

E: Sub-process /usr/bin/dpkg returned an error code (1) - but how do I find the meaningful error messages in APT's output?

How to avoid using System.String with Rfc2898DeriveBytes in C#

How can I train a replacement without letting my bosses and the replacement know?

How could China have extradited people for political reason under the extradition law it wanted to pass in Hong Kong?

Does C++20 mandate source code being stored in files?

Is there any road between the CA State Route 120 and Sherman Pass Road (Forest Route 22S0) that crosses Yosemite/Serria/Sequoia National Park/Forest?

What can I do to keep a threaded bolt from falling out of it’s slot

Story that includes a description: "Two concentric circles, intersecting at three points"

Is it allowable to use an organization's name to publish a paper in a conference, even after I graduate from it?

90s(?) book series about two people transported to a parallel medieval world, she joins city watch, he becomes wizard

How best to join tables, which have different lengths on the same column values which exist in both tables?

Do living authors still get paid royalties for their old work?

Default camera device to show screen instead of physical camera

Earliest evidence of objects intended for future archaeologists?

How to dismiss intrusive questions from a colleague with whom I don't work?



Find Two largest numbers in a list without using Array


How can I concatenate two arrays in Java?How do I join two lists in Java?Converting array to list in JavaWriting two dimensional array in javaHow can I “delimit” an integer from a given string?Java error in calculation when comparing largest and smallest numbers & sum with while loopEndless loop created if scanner receives something other than an intEntering (input/scanner) two same numbers consecutively to break a loop “while” Java






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















I'm Stuck with my homework. Basically,we need to Create a program to find the largest and smallest integers in a list entered by the user.And stops when the user enters 0. However we are not allowed to user arrays for this problem.



and one more condition : If the largest value appears more than once, that value should be listed as both the largest and second-largest value, as shown in the following sample run.



I have met the first condition of the program however I cannot meet the 2nd condition.



import java.util.Scanner;
public class FindTwoLargest
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int num = 1, max1st = 0, max2nd = 0;
int count = 1;
System.out.println("This program allows the user to create a list integers until the user enters 0");
System.out.println("The program will print the Largest value and the Second largest value from the list!");
while(num != 0)
System.out.print("Integer No." + count + " ");
num = sc.nextInt();

if(num >= max1st && num >= max2nd)
max1st = num;

if(num >= max2nd && num < max1st)
max2nd = num;

count++;

System.out.println("The largest value is " + max1st);
System.out.println("The second largest value is " + max2nd);





I have tried to use this code.



 if(num >= max2nd && num <= max1st) 
max2nd = num;



however when I run the program
https://imgur.com/a/YMxj9qm - this shows.



It should print 75 and 45 . What can I do to meet the first condition and meet the second condition?










share|improve this question
























  • I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

    – GhostCat
    8 hours ago











  • The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

    – Joshua Salcedo
    8 hours ago


















7















I'm Stuck with my homework. Basically,we need to Create a program to find the largest and smallest integers in a list entered by the user.And stops when the user enters 0. However we are not allowed to user arrays for this problem.



and one more condition : If the largest value appears more than once, that value should be listed as both the largest and second-largest value, as shown in the following sample run.



I have met the first condition of the program however I cannot meet the 2nd condition.



import java.util.Scanner;
public class FindTwoLargest
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int num = 1, max1st = 0, max2nd = 0;
int count = 1;
System.out.println("This program allows the user to create a list integers until the user enters 0");
System.out.println("The program will print the Largest value and the Second largest value from the list!");
while(num != 0)
System.out.print("Integer No." + count + " ");
num = sc.nextInt();

if(num >= max1st && num >= max2nd)
max1st = num;

if(num >= max2nd && num < max1st)
max2nd = num;

count++;

System.out.println("The largest value is " + max1st);
System.out.println("The second largest value is " + max2nd);





I have tried to use this code.



 if(num >= max2nd && num <= max1st) 
max2nd = num;



however when I run the program
https://imgur.com/a/YMxj9qm - this shows.



It should print 75 and 45 . What can I do to meet the first condition and meet the second condition?










share|improve this question
























  • I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

    – GhostCat
    8 hours ago











  • The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

    – Joshua Salcedo
    8 hours ago














7












7








7








I'm Stuck with my homework. Basically,we need to Create a program to find the largest and smallest integers in a list entered by the user.And stops when the user enters 0. However we are not allowed to user arrays for this problem.



and one more condition : If the largest value appears more than once, that value should be listed as both the largest and second-largest value, as shown in the following sample run.



I have met the first condition of the program however I cannot meet the 2nd condition.



import java.util.Scanner;
public class FindTwoLargest
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int num = 1, max1st = 0, max2nd = 0;
int count = 1;
System.out.println("This program allows the user to create a list integers until the user enters 0");
System.out.println("The program will print the Largest value and the Second largest value from the list!");
while(num != 0)
System.out.print("Integer No." + count + " ");
num = sc.nextInt();

if(num >= max1st && num >= max2nd)
max1st = num;

if(num >= max2nd && num < max1st)
max2nd = num;

count++;

System.out.println("The largest value is " + max1st);
System.out.println("The second largest value is " + max2nd);





I have tried to use this code.



 if(num >= max2nd && num <= max1st) 
max2nd = num;



however when I run the program
https://imgur.com/a/YMxj9qm - this shows.



It should print 75 and 45 . What can I do to meet the first condition and meet the second condition?










share|improve this question














I'm Stuck with my homework. Basically,we need to Create a program to find the largest and smallest integers in a list entered by the user.And stops when the user enters 0. However we are not allowed to user arrays for this problem.



and one more condition : If the largest value appears more than once, that value should be listed as both the largest and second-largest value, as shown in the following sample run.



I have met the first condition of the program however I cannot meet the 2nd condition.



import java.util.Scanner;
public class FindTwoLargest
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int num = 1, max1st = 0, max2nd = 0;
int count = 1;
System.out.println("This program allows the user to create a list integers until the user enters 0");
System.out.println("The program will print the Largest value and the Second largest value from the list!");
while(num != 0)
System.out.print("Integer No." + count + " ");
num = sc.nextInt();

if(num >= max1st && num >= max2nd)
max1st = num;

if(num >= max2nd && num < max1st)
max2nd = num;

count++;

System.out.println("The largest value is " + max1st);
System.out.println("The second largest value is " + max2nd);





I have tried to use this code.



 if(num >= max2nd && num <= max1st) 
max2nd = num;



however when I run the program
https://imgur.com/a/YMxj9qm - this shows.



It should print 75 and 45 . What can I do to meet the first condition and meet the second condition?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









Joshua SalcedoJoshua Salcedo

383 bronze badges




383 bronze badges















  • I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

    – GhostCat
    8 hours ago











  • The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

    – Joshua Salcedo
    8 hours ago


















  • I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

    – GhostCat
    8 hours ago











  • The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

    – Joshua Salcedo
    8 hours ago

















I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

– GhostCat
8 hours ago





I am not sure what you mean with "first condition". Your code shows how to "remind" "maximum" numbers. What prevents you from adding more variables, like min1st, and min2nd; and to set them up in similar ways?

– GhostCat
8 hours ago













The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

– Joshua Salcedo
8 hours ago






The first condition is to print the largest number and second largest number. I have done that part. However when the largest number appears twice . The largest number and the second largest number should be equal. That is what I dont get.The result should be like this : imgur.com/a/D2bkJIL However when I try running again but this time I did not repeat the largest number it prints this imgur.com/a/CmUHpxN . where it should be 35 and 26. I hope I am not confusing.

– Joshua Salcedo
8 hours ago













7 Answers
7






active

oldest

votes


















7














If you exceed your maximum number (max1st), your new maximum number will be set to num. But your second largest number will be the current maximum number. So try this condition:



if (num > max1st) 
max2nd = max1st;
max1st = num;
else if (num > max2nd)
max2nd = num;






share|improve this answer



























  • max2nd, not max2st :)

    – Avi
    8 hours ago











  • Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

    – Joshua Salcedo
    8 hours ago











  • @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

    – Vilx-
    7 mins ago



















3














Please user If else in those cases. You have used two if statements that is replacing the value of max2nd.



 import java.util.Scanner;
public class FindTwoLargest
public static void main(String args[])
Scanner sc = new Scanner(System.in);
int num = 1, max1st = 0, max2nd = 0;
int count = 1;
System.out.println("This program allows the user to create a list integers until the user enters 0");
System.out.println("The program will print the Largest value and the Second largest value from the list!");
while(num != 0)
System.out.print("Integer No." + count + " ");
num = sc.nextInt();

if(num >= max1st && num >= max2nd)
max1st = num;

else if(num >= max2nd && num < max1st)
max2nd = num;

count++;

System.out.println("The largest value is " + max1st);
System.out.println("The second largest value is " + max2nd);








share|improve this answer
































    0














    I changed the conditions, please take a look



    import java.util.Scanner;
    public class FindTwoLargest
    public static void main(String args[])
    Scanner sc = new Scanner(System.in);
    int num = 1, max1st = 0, max2nd = 0;
    int count = 1;
    System.out.println("This program allows the user to create a list integers until the user enters 0");
    System.out.println("The program will print the Largest value and the Second largest value from the list!");
    num = sc.nextInt();
    while(num != 0)
    System.out.print("Integer No." + count + " ");

    if(num > max1st)
    if(max1st > max2nd)
    max2nd = max1st;

    max1st = num;
    else
    if(num > max2nd)
    max2nd = num;


    count++;
    num = sc.nextInt();

    System.out.println("");
    System.out.println("The largest value is " + max1st);
    System.out.println("The second largest value is " + max2nd);








    share|improve this answer
































      0














      Here is a much simpler method.



      import java.util.Scanner;
      public class FindTwoLargest
      public static void main(String args[])
      Scanner sc = new Scanner(System.in);
      int num = 1, max1st = 0, max2nd = 0;
      do

      num = sc.nextInt();
      max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
      max1st = num > max1st ? num : max1st;

      while(num != 0)

      System.out.println("nThe largest value is " + max1st);
      System.out.println("The second largest value is " + max2nd);





      If you need any more explanation I will be happy to help.






      share|improve this answer
































        0














        You can try below approach, this should solve your issue =>



        import java.util.*;
        public class BackedList

        private static Scanner sc;

        public static void main(String args[])
        sc = new Scanner(System.in);
        int num = 1;
        List<Integer> integersList = new ArrayList<Integer>();
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        while(num != 0)
        System.out.print("Integer No." + count + " ");
        num = sc.nextInt();

        integersList.add(num);


        Collections.sort(integersList, Collections.reverseOrder());
        if(integersList.get(0) == integersList.get(1))
        System.out.println("The number " + integersList.get(0) + " is first largest and "
        + integersList.get(1) + " is the second largest number");

        else

        System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
        + integersList.get(integersList.size()-1));









        share|improve this answer
































          0














          1. When user inputs the numbers, add them to the List,

          2. Now sort the List in ascending order.


          3. The last element in List will be the maximum, and the second last
            will be the second maximum.



            List<Integer> List = Arrays.asList(2,5,7,90,12,56);
            List = List.stream().sorted(Integer::compareTo).collect(Collectors.toList());
            int size = List.size();
            System.out.println(List.get(size-1)+" "+List.get(size-2));






          share|improve this answer


































            -1














            import java.util.Scanner;
            public class FindTwoLargest
            public static void main(String args[])
            Scanner sc = new Scanner(System.in);
            int num = 1, max1st = 0, max2nd = 0;
            int count = 1;
            System.out.println("This program allows the user to create a list integers until the user enters 0");
            System.out.println("The program will print the Largest value and the Second largest value from the list!");
            while(num != 0)
            System.out.print("Integer No." + count + " ");
            num = sc.nextInt();

            if(num > max1st)
            max2nd=max1st;
            max1st = num;

            else if(num == max1st )
            max2nd = num;

            count++;

            System.out.println("The largest value is " + max1st);
            System.out.println("The second largest value is " + max2nd);





            you are over complicating things with && in your if. if a number is greater than 1st 1sr gets changed if a number is same as first 2nd gets changed if its not bigger or same nothing happens.






            share|improve this answer



























              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',
              autoActivateHeartbeat: false,
              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
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57557747%2ffind-two-largest-numbers-in-a-list-without-using-array%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              7














              If you exceed your maximum number (max1st), your new maximum number will be set to num. But your second largest number will be the current maximum number. So try this condition:



              if (num > max1st) 
              max2nd = max1st;
              max1st = num;
              else if (num > max2nd)
              max2nd = num;






              share|improve this answer



























              • max2nd, not max2st :)

                – Avi
                8 hours ago











              • Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

                – Joshua Salcedo
                8 hours ago











              • @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

                – Vilx-
                7 mins ago
















              7














              If you exceed your maximum number (max1st), your new maximum number will be set to num. But your second largest number will be the current maximum number. So try this condition:



              if (num > max1st) 
              max2nd = max1st;
              max1st = num;
              else if (num > max2nd)
              max2nd = num;






              share|improve this answer



























              • max2nd, not max2st :)

                – Avi
                8 hours ago











              • Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

                – Joshua Salcedo
                8 hours ago











              • @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

                – Vilx-
                7 mins ago














              7












              7








              7







              If you exceed your maximum number (max1st), your new maximum number will be set to num. But your second largest number will be the current maximum number. So try this condition:



              if (num > max1st) 
              max2nd = max1st;
              max1st = num;
              else if (num > max2nd)
              max2nd = num;






              share|improve this answer















              If you exceed your maximum number (max1st), your new maximum number will be set to num. But your second largest number will be the current maximum number. So try this condition:



              if (num > max1st) 
              max2nd = max1st;
              max1st = num;
              else if (num > max2nd)
              max2nd = num;







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 6 hours ago

























              answered 8 hours ago









              Torsten FehreTorsten Fehre

              4272 silver badges5 bronze badges




              4272 silver badges5 bronze badges















              • max2nd, not max2st :)

                – Avi
                8 hours ago











              • Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

                – Joshua Salcedo
                8 hours ago











              • @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

                – Vilx-
                7 mins ago


















              • max2nd, not max2st :)

                – Avi
                8 hours ago











              • Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

                – Joshua Salcedo
                8 hours ago











              • @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

                – Vilx-
                7 mins ago

















              max2nd, not max2st :)

              – Avi
              8 hours ago





              max2nd, not max2st :)

              – Avi
              8 hours ago













              Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

              – Joshua Salcedo
              8 hours ago





              Thanks.It solved my problem in a very simple way. I am still confused as to how the program knows why the the greatest number has been repeated twice.

              – Joshua Salcedo
              8 hours ago













              @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

              – Vilx-
              7 mins ago






              @JoshuaSalcedo - The program as it is currently written will find the two largest numbers, no matter what they are. If they happen to be the same number - ok, so be it. Or think about it in another way: as your program is running, at some point you will have two numbers as your max1st and max2nd. The first one will be the largest number ever, but the second one will be something smaller. Then comes along the other "largest number". Where will this code put it? That's right - in max2nd. After that no other number will be able to replace max1st or max2nd since they will all be smaller.

              – Vilx-
              7 mins ago














              3














              Please user If else in those cases. You have used two if statements that is replacing the value of max2nd.



               import java.util.Scanner;
              public class FindTwoLargest
              public static void main(String args[])
              Scanner sc = new Scanner(System.in);
              int num = 1, max1st = 0, max2nd = 0;
              int count = 1;
              System.out.println("This program allows the user to create a list integers until the user enters 0");
              System.out.println("The program will print the Largest value and the Second largest value from the list!");
              while(num != 0)
              System.out.print("Integer No." + count + " ");
              num = sc.nextInt();

              if(num >= max1st && num >= max2nd)
              max1st = num;

              else if(num >= max2nd && num < max1st)
              max2nd = num;

              count++;

              System.out.println("The largest value is " + max1st);
              System.out.println("The second largest value is " + max2nd);








              share|improve this answer





























                3














                Please user If else in those cases. You have used two if statements that is replacing the value of max2nd.



                 import java.util.Scanner;
                public class FindTwoLargest
                public static void main(String args[])
                Scanner sc = new Scanner(System.in);
                int num = 1, max1st = 0, max2nd = 0;
                int count = 1;
                System.out.println("This program allows the user to create a list integers until the user enters 0");
                System.out.println("The program will print the Largest value and the Second largest value from the list!");
                while(num != 0)
                System.out.print("Integer No." + count + " ");
                num = sc.nextInt();

                if(num >= max1st && num >= max2nd)
                max1st = num;

                else if(num >= max2nd && num < max1st)
                max2nd = num;

                count++;

                System.out.println("The largest value is " + max1st);
                System.out.println("The second largest value is " + max2nd);








                share|improve this answer



























                  3












                  3








                  3







                  Please user If else in those cases. You have used two if statements that is replacing the value of max2nd.



                   import java.util.Scanner;
                  public class FindTwoLargest
                  public static void main(String args[])
                  Scanner sc = new Scanner(System.in);
                  int num = 1, max1st = 0, max2nd = 0;
                  int count = 1;
                  System.out.println("This program allows the user to create a list integers until the user enters 0");
                  System.out.println("The program will print the Largest value and the Second largest value from the list!");
                  while(num != 0)
                  System.out.print("Integer No." + count + " ");
                  num = sc.nextInt();

                  if(num >= max1st && num >= max2nd)
                  max1st = num;

                  else if(num >= max2nd && num < max1st)
                  max2nd = num;

                  count++;

                  System.out.println("The largest value is " + max1st);
                  System.out.println("The second largest value is " + max2nd);








                  share|improve this answer













                  Please user If else in those cases. You have used two if statements that is replacing the value of max2nd.



                   import java.util.Scanner;
                  public class FindTwoLargest
                  public static void main(String args[])
                  Scanner sc = new Scanner(System.in);
                  int num = 1, max1st = 0, max2nd = 0;
                  int count = 1;
                  System.out.println("This program allows the user to create a list integers until the user enters 0");
                  System.out.println("The program will print the Largest value and the Second largest value from the list!");
                  while(num != 0)
                  System.out.print("Integer No." + count + " ");
                  num = sc.nextInt();

                  if(num >= max1st && num >= max2nd)
                  max1st = num;

                  else if(num >= max2nd && num < max1st)
                  max2nd = num;

                  count++;

                  System.out.println("The largest value is " + max1st);
                  System.out.println("The second largest value is " + max2nd);









                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 8 hours ago









                  Lone wolfLone wolf

                  53011 bronze badges




                  53011 bronze badges
























                      0














                      I changed the conditions, please take a look



                      import java.util.Scanner;
                      public class FindTwoLargest
                      public static void main(String args[])
                      Scanner sc = new Scanner(System.in);
                      int num = 1, max1st = 0, max2nd = 0;
                      int count = 1;
                      System.out.println("This program allows the user to create a list integers until the user enters 0");
                      System.out.println("The program will print the Largest value and the Second largest value from the list!");
                      num = sc.nextInt();
                      while(num != 0)
                      System.out.print("Integer No." + count + " ");

                      if(num > max1st)
                      if(max1st > max2nd)
                      max2nd = max1st;

                      max1st = num;
                      else
                      if(num > max2nd)
                      max2nd = num;


                      count++;
                      num = sc.nextInt();

                      System.out.println("");
                      System.out.println("The largest value is " + max1st);
                      System.out.println("The second largest value is " + max2nd);








                      share|improve this answer





























                        0














                        I changed the conditions, please take a look



                        import java.util.Scanner;
                        public class FindTwoLargest
                        public static void main(String args[])
                        Scanner sc = new Scanner(System.in);
                        int num = 1, max1st = 0, max2nd = 0;
                        int count = 1;
                        System.out.println("This program allows the user to create a list integers until the user enters 0");
                        System.out.println("The program will print the Largest value and the Second largest value from the list!");
                        num = sc.nextInt();
                        while(num != 0)
                        System.out.print("Integer No." + count + " ");

                        if(num > max1st)
                        if(max1st > max2nd)
                        max2nd = max1st;

                        max1st = num;
                        else
                        if(num > max2nd)
                        max2nd = num;


                        count++;
                        num = sc.nextInt();

                        System.out.println("");
                        System.out.println("The largest value is " + max1st);
                        System.out.println("The second largest value is " + max2nd);








                        share|improve this answer



























                          0












                          0








                          0







                          I changed the conditions, please take a look



                          import java.util.Scanner;
                          public class FindTwoLargest
                          public static void main(String args[])
                          Scanner sc = new Scanner(System.in);
                          int num = 1, max1st = 0, max2nd = 0;
                          int count = 1;
                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                          num = sc.nextInt();
                          while(num != 0)
                          System.out.print("Integer No." + count + " ");

                          if(num > max1st)
                          if(max1st > max2nd)
                          max2nd = max1st;

                          max1st = num;
                          else
                          if(num > max2nd)
                          max2nd = num;


                          count++;
                          num = sc.nextInt();

                          System.out.println("");
                          System.out.println("The largest value is " + max1st);
                          System.out.println("The second largest value is " + max2nd);








                          share|improve this answer













                          I changed the conditions, please take a look



                          import java.util.Scanner;
                          public class FindTwoLargest
                          public static void main(String args[])
                          Scanner sc = new Scanner(System.in);
                          int num = 1, max1st = 0, max2nd = 0;
                          int count = 1;
                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                          num = sc.nextInt();
                          while(num != 0)
                          System.out.print("Integer No." + count + " ");

                          if(num > max1st)
                          if(max1st > max2nd)
                          max2nd = max1st;

                          max1st = num;
                          else
                          if(num > max2nd)
                          max2nd = num;


                          count++;
                          num = sc.nextInt();

                          System.out.println("");
                          System.out.println("The largest value is " + max1st);
                          System.out.println("The second largest value is " + max2nd);









                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 8 hours ago









                          AdderAdder

                          4,1441 gold badge13 silver badges30 bronze badges




                          4,1441 gold badge13 silver badges30 bronze badges
























                              0














                              Here is a much simpler method.



                              import java.util.Scanner;
                              public class FindTwoLargest
                              public static void main(String args[])
                              Scanner sc = new Scanner(System.in);
                              int num = 1, max1st = 0, max2nd = 0;
                              do

                              num = sc.nextInt();
                              max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
                              max1st = num > max1st ? num : max1st;

                              while(num != 0)

                              System.out.println("nThe largest value is " + max1st);
                              System.out.println("The second largest value is " + max2nd);





                              If you need any more explanation I will be happy to help.






                              share|improve this answer





























                                0














                                Here is a much simpler method.



                                import java.util.Scanner;
                                public class FindTwoLargest
                                public static void main(String args[])
                                Scanner sc = new Scanner(System.in);
                                int num = 1, max1st = 0, max2nd = 0;
                                do

                                num = sc.nextInt();
                                max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
                                max1st = num > max1st ? num : max1st;

                                while(num != 0)

                                System.out.println("nThe largest value is " + max1st);
                                System.out.println("The second largest value is " + max2nd);





                                If you need any more explanation I will be happy to help.






                                share|improve this answer



























                                  0












                                  0








                                  0







                                  Here is a much simpler method.



                                  import java.util.Scanner;
                                  public class FindTwoLargest
                                  public static void main(String args[])
                                  Scanner sc = new Scanner(System.in);
                                  int num = 1, max1st = 0, max2nd = 0;
                                  do

                                  num = sc.nextInt();
                                  max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
                                  max1st = num > max1st ? num : max1st;

                                  while(num != 0)

                                  System.out.println("nThe largest value is " + max1st);
                                  System.out.println("The second largest value is " + max2nd);





                                  If you need any more explanation I will be happy to help.






                                  share|improve this answer













                                  Here is a much simpler method.



                                  import java.util.Scanner;
                                  public class FindTwoLargest
                                  public static void main(String args[])
                                  Scanner sc = new Scanner(System.in);
                                  int num = 1, max1st = 0, max2nd = 0;
                                  do

                                  num = sc.nextInt();
                                  max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
                                  max1st = num > max1st ? num : max1st;

                                  while(num != 0)

                                  System.out.println("nThe largest value is " + max1st);
                                  System.out.println("The second largest value is " + max2nd);





                                  If you need any more explanation I will be happy to help.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 8 hours ago









                                  RohitRohit

                                  15511 bronze badges




                                  15511 bronze badges
























                                      0














                                      You can try below approach, this should solve your issue =>



                                      import java.util.*;
                                      public class BackedList

                                      private static Scanner sc;

                                      public static void main(String args[])
                                      sc = new Scanner(System.in);
                                      int num = 1;
                                      List<Integer> integersList = new ArrayList<Integer>();
                                      int count = 1;
                                      System.out.println("This program allows the user to create a list integers until the user enters 0");
                                      System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                      while(num != 0)
                                      System.out.print("Integer No." + count + " ");
                                      num = sc.nextInt();

                                      integersList.add(num);


                                      Collections.sort(integersList, Collections.reverseOrder());
                                      if(integersList.get(0) == integersList.get(1))
                                      System.out.println("The number " + integersList.get(0) + " is first largest and "
                                      + integersList.get(1) + " is the second largest number");

                                      else

                                      System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
                                      + integersList.get(integersList.size()-1));









                                      share|improve this answer





























                                        0














                                        You can try below approach, this should solve your issue =>



                                        import java.util.*;
                                        public class BackedList

                                        private static Scanner sc;

                                        public static void main(String args[])
                                        sc = new Scanner(System.in);
                                        int num = 1;
                                        List<Integer> integersList = new ArrayList<Integer>();
                                        int count = 1;
                                        System.out.println("This program allows the user to create a list integers until the user enters 0");
                                        System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                        while(num != 0)
                                        System.out.print("Integer No." + count + " ");
                                        num = sc.nextInt();

                                        integersList.add(num);


                                        Collections.sort(integersList, Collections.reverseOrder());
                                        if(integersList.get(0) == integersList.get(1))
                                        System.out.println("The number " + integersList.get(0) + " is first largest and "
                                        + integersList.get(1) + " is the second largest number");

                                        else

                                        System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
                                        + integersList.get(integersList.size()-1));









                                        share|improve this answer



























                                          0












                                          0








                                          0







                                          You can try below approach, this should solve your issue =>



                                          import java.util.*;
                                          public class BackedList

                                          private static Scanner sc;

                                          public static void main(String args[])
                                          sc = new Scanner(System.in);
                                          int num = 1;
                                          List<Integer> integersList = new ArrayList<Integer>();
                                          int count = 1;
                                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                          while(num != 0)
                                          System.out.print("Integer No." + count + " ");
                                          num = sc.nextInt();

                                          integersList.add(num);


                                          Collections.sort(integersList, Collections.reverseOrder());
                                          if(integersList.get(0) == integersList.get(1))
                                          System.out.println("The number " + integersList.get(0) + " is first largest and "
                                          + integersList.get(1) + " is the second largest number");

                                          else

                                          System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
                                          + integersList.get(integersList.size()-1));









                                          share|improve this answer













                                          You can try below approach, this should solve your issue =>



                                          import java.util.*;
                                          public class BackedList

                                          private static Scanner sc;

                                          public static void main(String args[])
                                          sc = new Scanner(System.in);
                                          int num = 1;
                                          List<Integer> integersList = new ArrayList<Integer>();
                                          int count = 1;
                                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                          while(num != 0)
                                          System.out.print("Integer No." + count + " ");
                                          num = sc.nextInt();

                                          integersList.add(num);


                                          Collections.sort(integersList, Collections.reverseOrder());
                                          if(integersList.get(0) == integersList.get(1))
                                          System.out.println("The number " + integersList.get(0) + " is first largest and "
                                          + integersList.get(1) + " is the second largest number");

                                          else

                                          System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
                                          + integersList.get(integersList.size()-1));










                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 8 hours ago









                                          DurgeshDurgesh

                                          237 bronze badges




                                          237 bronze badges
























                                              0














                                              1. When user inputs the numbers, add them to the List,

                                              2. Now sort the List in ascending order.


                                              3. The last element in List will be the maximum, and the second last
                                                will be the second maximum.



                                                List<Integer> List = Arrays.asList(2,5,7,90,12,56);
                                                List = List.stream().sorted(Integer::compareTo).collect(Collectors.toList());
                                                int size = List.size();
                                                System.out.println(List.get(size-1)+" "+List.get(size-2));






                                              share|improve this answer































                                                0














                                                1. When user inputs the numbers, add them to the List,

                                                2. Now sort the List in ascending order.


                                                3. The last element in List will be the maximum, and the second last
                                                  will be the second maximum.



                                                  List<Integer> List = Arrays.asList(2,5,7,90,12,56);
                                                  List = List.stream().sorted(Integer::compareTo).collect(Collectors.toList());
                                                  int size = List.size();
                                                  System.out.println(List.get(size-1)+" "+List.get(size-2));






                                                share|improve this answer





























                                                  0












                                                  0








                                                  0







                                                  1. When user inputs the numbers, add them to the List,

                                                  2. Now sort the List in ascending order.


                                                  3. The last element in List will be the maximum, and the second last
                                                    will be the second maximum.



                                                    List<Integer> List = Arrays.asList(2,5,7,90,12,56);
                                                    List = List.stream().sorted(Integer::compareTo).collect(Collectors.toList());
                                                    int size = List.size();
                                                    System.out.println(List.get(size-1)+" "+List.get(size-2));






                                                  share|improve this answer















                                                  1. When user inputs the numbers, add them to the List,

                                                  2. Now sort the List in ascending order.


                                                  3. The last element in List will be the maximum, and the second last
                                                    will be the second maximum.



                                                    List<Integer> List = Arrays.asList(2,5,7,90,12,56);
                                                    List = List.stream().sorted(Integer::compareTo).collect(Collectors.toList());
                                                    int size = List.size();
                                                    System.out.println(List.get(size-1)+" "+List.get(size-2));







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 8 hours ago

























                                                  answered 8 hours ago









                                                  Vishwa RatnaVishwa Ratna

                                                  2,1953 gold badges15 silver badges35 bronze badges




                                                  2,1953 gold badges15 silver badges35 bronze badges
























                                                      -1














                                                      import java.util.Scanner;
                                                      public class FindTwoLargest
                                                      public static void main(String args[])
                                                      Scanner sc = new Scanner(System.in);
                                                      int num = 1, max1st = 0, max2nd = 0;
                                                      int count = 1;
                                                      System.out.println("This program allows the user to create a list integers until the user enters 0");
                                                      System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                                      while(num != 0)
                                                      System.out.print("Integer No." + count + " ");
                                                      num = sc.nextInt();

                                                      if(num > max1st)
                                                      max2nd=max1st;
                                                      max1st = num;

                                                      else if(num == max1st )
                                                      max2nd = num;

                                                      count++;

                                                      System.out.println("The largest value is " + max1st);
                                                      System.out.println("The second largest value is " + max2nd);





                                                      you are over complicating things with && in your if. if a number is greater than 1st 1sr gets changed if a number is same as first 2nd gets changed if its not bigger or same nothing happens.






                                                      share|improve this answer





























                                                        -1














                                                        import java.util.Scanner;
                                                        public class FindTwoLargest
                                                        public static void main(String args[])
                                                        Scanner sc = new Scanner(System.in);
                                                        int num = 1, max1st = 0, max2nd = 0;
                                                        int count = 1;
                                                        System.out.println("This program allows the user to create a list integers until the user enters 0");
                                                        System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                                        while(num != 0)
                                                        System.out.print("Integer No." + count + " ");
                                                        num = sc.nextInt();

                                                        if(num > max1st)
                                                        max2nd=max1st;
                                                        max1st = num;

                                                        else if(num == max1st )
                                                        max2nd = num;

                                                        count++;

                                                        System.out.println("The largest value is " + max1st);
                                                        System.out.println("The second largest value is " + max2nd);





                                                        you are over complicating things with && in your if. if a number is greater than 1st 1sr gets changed if a number is same as first 2nd gets changed if its not bigger or same nothing happens.






                                                        share|improve this answer



























                                                          -1












                                                          -1








                                                          -1







                                                          import java.util.Scanner;
                                                          public class FindTwoLargest
                                                          public static void main(String args[])
                                                          Scanner sc = new Scanner(System.in);
                                                          int num = 1, max1st = 0, max2nd = 0;
                                                          int count = 1;
                                                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                                                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                                          while(num != 0)
                                                          System.out.print("Integer No." + count + " ");
                                                          num = sc.nextInt();

                                                          if(num > max1st)
                                                          max2nd=max1st;
                                                          max1st = num;

                                                          else if(num == max1st )
                                                          max2nd = num;

                                                          count++;

                                                          System.out.println("The largest value is " + max1st);
                                                          System.out.println("The second largest value is " + max2nd);





                                                          you are over complicating things with && in your if. if a number is greater than 1st 1sr gets changed if a number is same as first 2nd gets changed if its not bigger or same nothing happens.






                                                          share|improve this answer













                                                          import java.util.Scanner;
                                                          public class FindTwoLargest
                                                          public static void main(String args[])
                                                          Scanner sc = new Scanner(System.in);
                                                          int num = 1, max1st = 0, max2nd = 0;
                                                          int count = 1;
                                                          System.out.println("This program allows the user to create a list integers until the user enters 0");
                                                          System.out.println("The program will print the Largest value and the Second largest value from the list!");
                                                          while(num != 0)
                                                          System.out.print("Integer No." + count + " ");
                                                          num = sc.nextInt();

                                                          if(num > max1st)
                                                          max2nd=max1st;
                                                          max1st = num;

                                                          else if(num == max1st )
                                                          max2nd = num;

                                                          count++;

                                                          System.out.println("The largest value is " + max1st);
                                                          System.out.println("The second largest value is " + max2nd);





                                                          you are over complicating things with && in your if. if a number is greater than 1st 1sr gets changed if a number is same as first 2nd gets changed if its not bigger or same nothing happens.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered 8 hours ago









                                                          Slobodan MargetićSlobodan Margetić

                                                          12 bronze badges




                                                          12 bronze badges






























                                                              draft saved

                                                              draft discarded
















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid


                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.

                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57557747%2ffind-two-largest-numbers-in-a-list-without-using-array%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                                              François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480