• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Largest and smallest number

 
wei qi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to express with "if (input != min) " ?
 
wei qi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was just messing around. i'm a beginner to java. it should be "<"
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that initializing min and max to 0 will not cause any problem ?
 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to use the code button. It makes it easier to read.

 
Jacob Coddaire
Ranch Hand
Posts: 36
Chrome Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you just do the following?

Instead of


try this:
 
wei qi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you the 2nd? (first time the user inputs a value, initialize both min and max to that value)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After the first "input = keyboard.nextInt();", set both min and max to the input value.
 
wei qi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheese and rice it works!! thank you!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Coddaire wrote:try this:

min = min? Why would you ever want to assign a variable to itself?
 
Jordan Collins
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10090
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 48381
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic