• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Find Highest and Smallest Number

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used below code to find greatest and smallest number, but the output for the smallest value is wrong.
 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't run it but it looks correct to me, maybe you wanted if (score <= lowestScore) ?
 
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have got the algorithm for lowest numbers wrong. Start by working out what will happen if you pass a poor mark (e.g. 20) as the first input in that method. Then see where the error is. The output for the smallest value should give you a hint where the mistake is.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the output

Enter number of students: 3
Enter student's name: tong
Enter tong 's score:
45
Enter student's name: Michelle
Enter Michelle 's score:
87
Enter student's name: Chris
Enter Chris 's score:
77
Michelle get highest score : 87.0
get lowest score : 77.0

There are two erros here.
1.  It get the last value as the smallest
2. The lowestName not getting displayed.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strange, the lowestNumber get displayed now, but the output still incorrect
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:I didn't run it but it looks correct to me, maybe you wanted if (score <= lowestScore) ?

I tried, the output still same
 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I have seen another error, which only affects the low score. Changing < to <= won't make any difference.
 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, maybe you will see a difference from <=. The output will be even more confusing.
Go through the algorithm to work out the highest score, and compare it with the other algorithm, which should be the same but backwards, if you see what I mean.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Oh, I have seen another error, which only affects the low score. Changing < to <= won't make any difference.

Can you tell what's wrong here ?  
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Go through the algorithm to work out the highest score, and compare it with the other algorithm, which should be the same but backwards, if you see what I mean.

Noted
 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:. . . Can you tell what's wrong here ?  

Yes.

Will I tell you what is wrong? No.

You really need to work out this sort of thing for yourself; you cannot learn from somebody else giving you the answers.
 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you can do is write everything that happens on a sheet paper and trace through the program as if it were running, go line by line to see what happens to each variable, write it out on the paper every time it changes. This is how you would have to do it for a test at school. Another way is to set a breakpoint in your IDE and use the debugger, watch the values of the variable change as you step through the program line by line. Either way you do it, you will be able to figure out whats wrong with the program. It's good to learn how to debug your programs yourself.
 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:What you can do is . . .

Agree

Another thing is to write down what you are doing with the largest value algorithm, and compare the two. Remember the largest value algorithm is working correctly.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
coding actually requires you to spend 90% of your time thinking, and 10% of your time typing.  My guess is you have these values reversed.  You shouldn't type a single character until you have spent a good chunk of time figuring out what exactly your program should do.

The best advice I can give you is to write out, step by step, exactly how you want to find the largest and smallest numbers.

THINK through every decision, including what you initialize your variables to...Why did you initialize highestScore to 0?  is that correct? Is there a better value you could initialize it to?
 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He will get an NPE if he does what I think you are suggesting fred, then he might be able to read the stack trace to see exactly where his algorithm is broken. Right now he has the worst kind of error, a logic error.
 
Sheriff
Posts: 9012
655
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP

Rename variable “score” in all places you have it to “currentScore” by thinking during each rename.

Please tell me what you think about the each line you changed.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the suggestions guys! This is my latest code and final output

Output

Enter number of students: 3
Enter student's name: t
Enter t 's score:
34
Enter student's name: w
Enter w 's score:
78
Enter student's name: s
Enter s 's score:
55
t get highest score : 78.0
get lowest score : 34.0
BUILD SUCCESSFUL (total time: 16 seconds)


Any ideas why the lowestName not getting displayed at the end ?
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know why the lowestName not getting displayed now. I forgot to set it.
This is the working code
 
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are running into a Scanner gotcha. For name you probably want to use nextLine() instead of next(). It will accept spaces and prevent new-line characters from being left in the queue. Also, after your call to nextDouble() you'll want to immediately call nextLine() and ignore the return. This will purge the new-line left over from your call to nextDouble().
 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not convinced that is the reason for getting a blank for lowestName. You can only get an empty String from Scanner#next by changing the delimiter.
OP: Why are you using doubles when scores are usually ints.
I don't like − 1 like that in your loop. Use 1 as the initial value and remove the − 1.
You still have an error in that code. Let's see if I can't get that error to produce a wrong output:-

java Chapter4
Enter number of students: 3
Enter student's name: Carey 9865329659827365962349574
Enter Carey 's score:
Enter student's name: John 56
Enter John 's score:
Enter student's name: Campbell 3
Enter Campbell 's score:
  get highest score : 9.865329659827367E24
Campbell get lowest score : 3.0

Notice that a Scanner can take the input before you print the message: that is normal.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S Fox wrote:He will get an NPE if he does what I think you are suggesting fred, then he might be able to read the stack trace to see exactly where his algorithm is broken. Right now he has the worst kind of error, a logic error.


All i am suggesting is that the OP think through every decision.  I gave one example of a decision made that was most likely NOT thought through - it's pretty common to initialize all variables to 0 or "" or whatever. There are probably other, similar not-thought-through-decisions made with other variables in the code.
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Complete Code
Output

Enter number of students: 3
Enter student's name: Tong
Enter Tong 's score: 23
Enter student's name: Michelle
Enter Michelle 's score: 78
Enter student's name: John
Enter John 's score: 45
Michelle get highest score : 78.0
Tong get lowest score : 23.0

 
Campbell Ritchie
Marshal
Posts: 80624
470
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Joe wrote:Complete Code . . .

Afraid not. You haven't corrected the error I found, not even after Carey pointed out what it was. It only shows up if the highest score is entered first.
 
S Fox
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm starting to feel bad for this guy! Don't give up, keep testing it with various input scenarios. Testing it only one way is not good.
 
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a quick suggestion, remove the imports you are not using anymore.
these are not being used by your program.
 
Mishra Saurabh
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit : maybe i didn't see your latest code, if you have already done it. Ignore the suggestion
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another try
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congrats. It appears that you've achieved your goals.

Something to consider is what you would have done differently to make the user interface bullet proof.

What if the user enters a string where a number is expected? Or vice versa?
What if num is less than one?
What if you were asked to continue entering names and scores until an empty name was entered?

An approach that is often taken with a min/max problems is to initialize them like this:
With this the first entry compared against these would always be true (except if they actually did enter those min/max values).

One thing that was mentioned and is worth repeating: you have to test using a number of scenarios including those that should fail.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rafaela Bedim wrote:oi

Yes?
Content minimized. Click to view
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of having a second if statement you will need to replace it with a else if so it wont have to check both statements each time you run it.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if you turn this part:

into an if else statement would that change anything?
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
reply
    Bookmark Topic Watch Topic
  • New Topic