This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

My loop always returns only the largest num

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I made a code which takes all user inputs and then adds it into an array list. I am fine with what I have there. I am confused as to why the for loop returns the largest number even though my logic tells me it should return the second smallest one. Any help is appreciated!
 
Rancher
Posts: 4450
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 23 and 24 are the wrong way round.
You are setting largest to the new value then setting secondLargest to the value of largest, so they're always the same.
 
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if you have several values equal to the largest?

And welcome to the Ranch
 
Dave Tolls
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought that, but then I think this handles it?
Unless all the numbers are the same...that's the only possible edge case issue I can see (with a not-too-deep look, it has to be said).

Oh, and negative numbers (that aren't -1).
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem about −1 comes from misunderstanding the loop in line 7. All you have to do is enter a non‑integer and the loop terminates. No need for −1 or break; or anything like that.
 
Marshal
Posts: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The problem about −1 comes from misunderstanding the loop in line 7. All you have to do is enter a non‑integer and the loop terminates. No need for −1 or break; or anything like that.



Right. But what if I wanted to use -1 as one of the numbers I'm inputting? Or some other negative numbers? The posted code won't handle those properly.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...while (input.hasNextInt())... will stop the loop nicely as soon as you enter a non‑int.
 
Dave Tolls
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but even with that fix the posted code won't handle negative numbers correctly.

Entering:
-1 -2 -3 -4 -5

will result in an answer of 0.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You pointed out problems with negative numbers before. I shall try on JShell, having taken the if containing break; out.

Now I can see why you are getting problems with negative numbers. You were correct about lines 23‑24. Once you correct those two problems, you get the number one smaller than the largest number, which isn't necessarily the second largest:-

Enter an int value: 736583 -1 -2 -3 -1 736583 -734 -2147483648 x
...
Second largest number is: -1 and it is located at: 1

It should be possible to find largest and smallest with only one loop.
 
Dave Tolls
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you get -1 as a result, when both values start at 0?
I must be reading the code incorrectly or something:

If you stick your values in there then I don't see how secondLargest can ever be -1.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:How do you get -1 as a result, when both values start at 0? . . . .

Sorry for causing such confusion. I did say, “Once you correct those two problems”. I had corrected the problem about starting the values at 0, replacing 0 by 0x8000_0000.
I also tried with a Stream, like this:-There are still potential problems with that code if you have < 2 entries. You won't get the right indices if there are two instances of the largest number. Actually, if you sort the List, you won't get the right indices full stop.
 
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How's this solution (Excluded the scanner for brevity) ?

Works for 1 element, 2 elements, also works if all elements are the same.
 
pshivvy pat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Campbell Ritchie wrote:The problem about −1 comes from misunderstanding the loop in line 7. All you have to do is enter a non‑integer and the loop terminates. No need for −1 or break; or anything like that.



Right. But what if I wanted to use -1 as one of the numbers I'm inputting? Or some other negative numbers? The posted code won't handle those properly.



I apologize for the late reply. -1 is there as a value which we were instructed to stop at. If userInput is -1, the loop stops. Otherwise, it runs.
 
Paul Clapham
Marshal
Posts: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So I could enter -2 and -4 and -17 and your code would go on to find the second largest of those three numbers? What would it output?
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pshivvy pat wrote:. . . -1 is there as a value which we were instructed to stop at. . . .

That sort of code suggests you are only dealing with natural numbers. I don't like to see such sentinel values used in case they are confused with real input. One day you will have a loop which should accept integers, positive and negative. If I had to exclude negative numbers, I would probably write a loop as follows. Note the extra pair of {} because i was declared outside the loop and the {} restrict its scope.What happens is that input reads an int and assigns it to i. That needs a pair of () to ensure the = operator runs before >=. Then the test for non‑negativity is done; i goes out of scope at line 8.
The extra () and = make for strange‑looking syntax, but it is a useful trick. If you use a buffered reader to read the lines in a file, you can do something siimilar:-
 
pshivvy pat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

pshivvy pat wrote:. . . -1 is there as a value which we were instructed to stop at. . . .

That sort of code suggests you are only dealing with natural numbers. . . .



I think there is a misunderstanding. The program has to stop if a user -1, that was what our rules stated.
 
pshivvy pat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay. So I could enter -2 and -4 and -17 and your code would go on to find the second largest of those three numbers? What would it output?



It returns 0 if only negative values are entered, unless it is -1. Then, it stops.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pshivvy pat wrote:

I made a code which takes all user inputs and then adds it into an array list. I am fine with what I have there. I am confused as to why the for loop returns the largest number even though my logic tells me it should return the second smallest one. Any help is appreciated!


you must sort data first, then select inex-1 value

sorting need 2 for loops (and swapping process inside the inner loop)
do not forget to care about various Exceptions, like type of inputted data, size of index etc...
 
Mohammed Al-otaibi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I run your code
it is working fine but with delibrate number entry
it gives erroneous results with other mixed values
 
pshivvy pat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Al-otaibi wrote:. . . it is working fine but with delibrate number entry
it gives erroneous results with other mixed values



Could you tell me a set your tried?
 
Mohammed Al-otaibi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and from mental math and logic point you should alter two lines:

               largest = userInputVals.get(i);//11
               secondLargest = largest;


to

               secondLargest = largest;
               largest = userInputVals.get(i);


put this line first secondLargest = largest; to retain old value of largest then update largest with new largest value

what you are doing in your code, is detecting max then immediately assign it to secondLargest!!

               
 
Mohammed Al-otaibi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and you had better to include any repletion of second largest number
 
Mohammed Al-otaibi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pshivvy pat wrote:. . . Could you tell me a set your tried?


we already discovered thr problem
it is not programmatic rather than mental-thinking

the se one should retail old largest value, not updated one.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

pshivvy pat wrote:. . . It returns 0 if only negative values are entered . . .

So an input comprising only negative numbers will not cause it to return the second largest input. In that case, you have been given instructions which produce incorrect results from some inputs.
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammed Al-otaibi and pshivvy pat:  Please only quote enough of the conversation to provide context.  If you're replying to the last post this sometime means don't quote at all.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammed Al-otaibi wrote:and from mental math and logic point you should alter two lines: . . . .

Dave Tolls has already said that.

the se one should retail old largest value, not updated one.

What does that mean? It doesn't add anything useful to the discussion..
 
Sheriff
Posts: 7421
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Why do you let user to enter negative values other than -1 at all? Does it make sense to you, givent that -1 is an exit value..?
 
Liutauras Vilda
Sheriff
Posts: 7421
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If user would enter -1 as a first entry, how he could figure out that program actually works?  Enter -1 and it exits without any indication why.
 
It was the best of times. It was the worst of times. It was a tiny ad.
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!