aspose file tools*
The moose likes Beginning Java and the fly likes Largest and smallest number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Largest and smallest number" Watch "Largest and smallest number" New topic
Author

Largest and smallest number

wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
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.





Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What do you want to express with "if (input != min) " ?


[My Blog]
All roads lead to JavaRanch
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
i was just messing around. i'm a beginner to java. it should be "<"
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Are you sure that initializing min and max to 0 will not cause any problem ?
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Be sure to use the code button. It makes it easier to read.

Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Can't you just do the following?

Instead of


try this:
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
thank you for your reply!

i tried it and it still doesn't work.

i have a question about initializing min, max, and input. do i have to put int min(max,input) = 0? i feel that THAT is the problem but if i don't set it to a value it won't compile
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

There are several ways to initialize them :
1. Use Integer.MIN_VALUE and Integer.MAX_VALUE
2. Or, the first time the user inputs a value, initialize both min and max to that value
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
how do you the 2nd? (first time the user inputs a value, initialize both min and max to that value)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

After the first "input = keyboard.nextInt();", set both min and max to the input value.
wei qi
Greenhorn

Joined: Feb 24, 2011
Posts: 5
cheese and rice it works!! thank you!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14423
    
  23

Jacob Coddaire wrote:try this:

min = min? Why would you ever want to assign a variable to itself?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jordan Collins
Greenhorn

Joined: Oct 03, 2013
Posts: 1
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Welcome to the Ranch

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8400
    
  23

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.

    HIH

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40029
        
      28
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Largest and smallest number