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;
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
add a comment |
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
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
add a comment |
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
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
java
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
add a comment |
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
add a comment |
7 Answers
7
active
oldest
votes
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;
max2nd, notmax2st:)
– 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 yourmax1standmax2nd. 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 - inmax2nd. After that no other number will be able to replacemax1stormax2ndsince they will all be smaller.
– Vilx-
7 mins ago
add a comment |
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);
add a comment |
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);
add a comment |
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.
add a comment |
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));
add a comment |
- When user inputs the numbers, add them to the
List, - Now sort the
Listin ascending order. The last element in
Listwill 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));
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
max2nd, notmax2st:)
– 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 yourmax1standmax2nd. 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 - inmax2nd. After that no other number will be able to replacemax1stormax2ndsince they will all be smaller.
– Vilx-
7 mins ago
add a comment |
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;
max2nd, notmax2st:)
– 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 yourmax1standmax2nd. 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 - inmax2nd. After that no other number will be able to replacemax1stormax2ndsince they will all be smaller.
– Vilx-
7 mins ago
add a comment |
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;
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;
edited 6 hours ago
answered 8 hours ago
Torsten FehreTorsten Fehre
4272 silver badges5 bronze badges
4272 silver badges5 bronze badges
max2nd, notmax2st:)
– 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 yourmax1standmax2nd. 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 - inmax2nd. After that no other number will be able to replacemax1stormax2ndsince they will all be smaller.
– Vilx-
7 mins ago
add a comment |
max2nd, notmax2st:)
– 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 yourmax1standmax2nd. 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 - inmax2nd. After that no other number will be able to replacemax1stormax2ndsince 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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered 8 hours ago
Lone wolfLone wolf
53011 bronze badges
53011 bronze badges
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered 8 hours ago
AdderAdder
4,1441 gold badge13 silver badges30 bronze badges
4,1441 gold badge13 silver badges30 bronze badges
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 8 hours ago
RohitRohit
15511 bronze badges
15511 bronze badges
add a comment |
add a comment |
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));
add a comment |
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));
add a comment |
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));
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));
answered 8 hours ago
DurgeshDurgesh
237 bronze badges
237 bronze badges
add a comment |
add a comment |
- When user inputs the numbers, add them to the
List, - Now sort the
Listin ascending order. The last element in
Listwill 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));
add a comment |
- When user inputs the numbers, add them to the
List, - Now sort the
Listin ascending order. The last element in
Listwill 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));
add a comment |
- When user inputs the numbers, add them to the
List, - Now sort the
Listin ascending order. The last element in
Listwill 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));
- When user inputs the numbers, add them to the
List, - Now sort the
Listin ascending order. The last element in
Listwill 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));
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
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 8 hours ago
Slobodan MargetićSlobodan Margetić
12 bronze badges
12 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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