This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
Hi everyone. I am trying to write a java program that allows the user to input as many integer values. At the end, it is supposed to figure out which is the largest and smallest number. -99 is used to quit if the user no longer wants to put in anymore values. Also, if -99 is entered in the first time, it will say "You did not enter any numbers". I wrote this code but am having a hard time getting it figure out which is the smallest.
I'm just a noob, but this is what I came up with for my own problem in class that was similar to this one...
Okay, so... I probably overused the != -99 a bit, but oh well!~ I did what was asked of me in the Programming Challenges on Chapter 4 of the book. != -99 was to end the input at any point in the loop, which is a bit different from what you're doing.
I am afraid you have some serious problems. Using those two nested loops is a minor mistake. The most dangerous is closing a Scanner pointing to System.in.Write a class which uses that code, and see what happens when you reach line 5.
What is going to happen if you enter some numbers like 1 2 69 4? Go through those numbers and see what happens if you set the initial value for the minimum to 0. You can get similar problems with maximum if you enter negative values only.
Your method is too long. A lot of it can be deleted, including the bit about not entering any numbers. The only way you can reach that is by entering 0, which is a valid input.
You also appear to be requesting the same number twice, in a third level of looping.
Jordan Collins wrote:Okay, so... I probably overused the != -99 a bit, but oh well...
I'm afraid both you and wei have fallen into the trap that many beginners do: not knowing when to StopCoding (←click).
You can't just expect the solution to magically appear, you need to understand the problem first; so if the first thing you do is to sit down and start writing Java code, the chances are your solution will be poorly written (and probably over-written).
Think of it as a kid's game with wooden blocks with numbers on. You hand the kid the blocks, one at a time, and s/he has to keep the the lowest and highest of the ones s/he's been given until you stop. What does the kid have to do?
Write it down in English (or your native language), because that's the essence of the problem. All the other stuff about Scanners, and loops, and conditions, and how to input numbers, is all eyewash; and it's a distraction. You definitely need it to get your program working properly, but it's not what makes this program/problem unique.
My advice: Forget about this particular problem for the moment, and familiarise yourself with the business of using Scanner.
Write some test programs that take one value, or 3, or 10, or any number until you enter "quit" (or -99); and for the moment, don't worry about what you're going to do with those values; just print them out, so you can see they were received OK.
Make sure you can get strings, and numbers, and single characters, or anything else you can think of. Try and use as many of the "next..." and "hasNext..." methods as you can, so that you really understand the API.
The reason? Because user input is something you have to do A LOT - at least when you're starting out - and it's tricky and fiddly.
But it almost always follows the same patterns:
Input 3 numbers (or 5, or 10).
Input customers names until the user types "quit".
Input values until one of them matches something previously stored in the program.
and getting a handle on how to use Scanner properly really gives you a good head start on how to write all sorts of programs like that.
And believe me, by the time you've become an "expert" in using Scanner, the business of "min" and "max" will be a walk in the park.
posted 5 years ago
Winston and I have differing opinions about Scanner. I like it, he doesn't. It is particularly my point 2 he dislikes; it is poorly described in many books, or omitted altogether.
There are three common problems with Scanner
1: Knowing when to close it; that means every time you are not using System.in
2: Using nextLine() after something other than nextLine(). You may have to call nextLine() twice. More details here.
3: Handling incorrect input.
When you have time, I recommend you create a utility class. When I started Java™, we all wrote ourselves utility classes to read from the keyboard, and I was told that they were no longer necessary now Scanner is available. I would disagree. I have a cut‑down version here. You can enhance that to send a custom error message, or to insist on minimum and maximum numbers or get different kinds of data. You would only have one Scanner instacne pointing to System.in and you never need close it. But you may have to synchronise all methods if you ever use it in a multi‑threaded environment.