How to determine smallest and largest number in Java

Numbers
Numbers

A couple of weeks ago a reader asked me for a Java program to read command-line input from the user and then display the smallest value the user has entered. He wanted to use digit ‘0’ to indicate that the user is done entering the data and not to include 0 in the list of numbers entered by the user. Here is the Java code to do this. Bonus: This program also determines the largest value entered by the user.



import java.io.*;
import java.util.*;
public class ReadInputNumbers {
      public static void main (String[] args) {
      System.out.print("Enter numbers. Enter 0 after you have
entered  all numbers. \n");
      BufferedReader br = new BufferedReader( 
                               new InputStreamReader(System.in));
      String numberStr = "1";
      ArrayList inputNumbers = new ArrayList();
      Integer intNumber =-999;		
      while(intNumber != 0){
 	 try {
		numberStr = br.readLine();
		intNumber = new Integer(numberStr);
		if(intNumber != 0){
		   inputNumbers.add(new Integer(numberStr));
		}
	} catch (IOException ioe) {
	   System.out.println("IO error trying to read input!");
	   System.exit(1);
	}catch(NumberFormatException e){
	   System.out.println("You entered an invalid number 
                                   ("+numberStr+").");
	   System.exit(1);
	}
      }
      Collections.sort(inputNumbers);
      Iterator itr = inputNumbers.iterator();
      if(!inputNumbers.isEmpty()){
	 System.out.println("You entered..." + inputNumbers);
	 System.out.println("Smallest value you entered is " + 
                         inputNumbers.get(0));
 	 System.out.println("Largest value you entered is " + 
                        inputNumbers.get(inputNumbers.size()-1));
	}else{
	  System.out.println("You did not enter any number.");
	}
   }
} 



Enjoy.

Image credit:BaylorBear78

Comments 31

  • fantastic submit, very informative. I’m wondering why the other experts of this sector don’t understand this.
    You should proceed your writing. I’m sure, you’ve a great readers’ base already!

  • The library is another useful supply of data on how to care
    for your pet. Because of the hurricanes that can sweep up and
    down Florida’s coastline pet owners in Florida should consider
    purchasing a pet health insurance plan that will
    protect their pets in event of a hurricane. If your cat has a chronic condition, catheterization or surgery
    might be recommended.

  • I’m really enjoying the design and layout of your website.

    It’s a very easy on the eyes which makes it much more enjoyable for me to come here and
    visit more often. Did you hire out a developer
    to create your theme? Excellent work!

  • I am genuinely grateful to the owner of this web page who has shared this wonderful paragraph at at this time.

  • Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much.
    I hope to give something back and help others like you helped
    me.

  • If some one wants expert view regarding running a blog then i
    advise him/her to go to see this weblog, Keep up the fastidious job.

  • I needed to thank you for this wonderful read!!
    I definitely enjoyed every bit of it. I have
    you book-marked to check out new things you post…

  • Yes! Finally someone writes about jabong discount coupons on watches.

  • Nice post.I was checking continuously this weblog and I’m impressed!
    Extremely helpful information particularly the ultimate part 🙂 I take care oof such information much.
    I was seeking this certain information for a very long time.

    Thaank you and good luck.

  • Thank you for sharing your info. I truly appreciate your efforts and I will be waiting for your further
    post thank you once again.

  • Since the admin of this web page is working, no doubt very quickly it
    will bbe well-known, ddue to its quality contents.

  • I am now not sure the place you’re getting your info, but great
    topic. I must spend some time finding out much more or understanding more.
    Thanks for wonderful info I used to be looking for this info for
    my mission.

    Here is my page … medical negligence firms

  • Great post. I was checking constantly this blog and I am impressed!
    Extremely helpful information specifically the last part 🙂 I care for such info a lot.

    I was looking for this certain info for a long time.
    Thank you and good luck.

  • whoah this weblog is fantastic i like reading your
    posts. Stay up the great work! You already know, a lot of individuals are looking around for this info, you could help them greatly.

  • Thanks for a marvelous posting! I truly enjoyed reading it, you will be a great author.I will make sure
    to bookmark your blog and may come back very
    soon. I want to encourage continue your
    great work, have a nice holiday weekend!

  • wonderful issues altogether, you just received a new
    reader. What could you suggest about your post that you just made some days
    in the past? Any positive?

  • I enjoy, result in I found exactly what I was taking a look for.
    You have ended my 4 day long hunt! God Bless you man. Have
    a great day. Bye

  • Haave you ever considered about adding a little bit
    more thawn just your articles? I mean, what yyou say
    is valuable and everything. But imagine iff you added some great pictures or video clips to give your posts more, “pop”!
    Your content is excellent but with pics and video clips, this website could definitely be one of thee very best iin its niche.
    Great blog!

  • My family members always say that I am killing my time her at net, however I know I am getting experience everyday by reading thes fastidious posts.

  • It’s a pityy you don’t have a donate button! I’d certainly
    donate to this superb blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to new updates and ill share this website with my
    Facebook group. Talkk soon!

  • Hello, i read yyour blog from tim to time and i own a similar one and
    i was just wondering if you get a lot of spam remarks? If so hoow do you stop it, any plugin or anything
    you can advise? I get so much lately it’s driving me crazy so any assistance
    is very much appreciated.

  • Hi there! I ccould have sworn I’ve been to this site befgore but
    after checking through some of thhe post
    I realized it’s new to me. Anyhow, I’m definitely happy I found
    itt and I’ll be bookmarking and checking back often!

  • If you are going for finest contents like I do, simply pay a visit this site
    every day for the reason that it presents feature contents, thanks

  • My spouse and I absolutely love your blog and find almost all of your post’s to be just what I’m looking for. Does one offer guest writers to write content for you? I wouldn’t mind publishing a post or elaborating on many of the subjects you write regarding here. Again, awesome website!

  • I think this is among the most significant info for me.

    And i am glad reading your article. But wanna remark on few general things, The web site style is
    wonderful, the articles is really nice : D.
    Good job, cheers

  • pleasant document which is educational, we will be
    needing this content similar to this a lot more, hope you could talk about to be able to
    all of us a lot more about excellent informatif data.

  • 4J Studios prevciously stated that new patches
    should bbe coming out at approximately the same time on the PS4, Xbox One, PS3, Xbox 360 and Vita.
    Due to the host of new funnctions aand enhancemnents each newcomers and seasoned customers will seem ahead the each and every and
    each annd every session. It’s bad too rest doing the very same point for lengthy durations of
    time.

  • Most of the apps are broad because variety of members are registered eyeing for the
    diverse types of relationships. Amongst the most wll liked styles of interracial
    romancing in America right now involves whites and blacks seeking dates.
    Until you really get to know the person you do not want to reveal too much.

  • As Android is a powerful and related platform for mobile applications so itt is more
    preferre over other technologies. This app is fast as well as stable, amijd a
    straightforwardness thyat reminds of the older dys while using
    Facebook was bearable. As technology has become more
    and more sophisticated, society has increasingly grown dependent and infatuated with its astounding capabilities.

  • Good way of describing, and nice piece of writing to obtain information concerning my presentation focus, which i am going
    to convey in college.

Leave a Reply

Your email address will not be published. Required fields are marked *