-1

I have a slight issue with my program. I need to ask the user to input as many numbers as they want and then the program will tell them what is the smallest and largest number. My issue is when all is said and done it prints out "the largest number is 0" and "the smallest number is 0". It always says that even if i never enter 0. I was wondering what was wrong with the program. Any pointers or helpers would be fantastic. Again to repeat, the issue im having is that the smallest and largest come back as 0's no matter what.

import java.util.Scanner;
public class LargestAndSmallest {

    public static void main(String[] args) {
        int smallest = 0;
        int large = 0;
        int num;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the numer");
        int n = keyboard.nextInt();
        num = keyboard.nextInt();
        while (n != -99) {
            System.out.println("Enter more numbers, or -99 to quit");
            n = keyboard.nextInt();
        }

        for (int i = 2; i < n; i++) {
            num = keyboard.nextInt();

            if (num > large) {
                large = num;
                System.out.println(large);
            }

            if (num < smallest) {
                smallest = num;
            }
        }
        System.out.println("the largest is " + large);
        System.out.println("the smallest is " + smallest);
    }
}

I used this code as in the first place: Java program to find the largest & smallest number in n numbers without using arrays

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
John Smth
  • 31
  • 2
  • 11
  • don't read in the number from the user twice each time you ask for an input – RAZ_Muh_Taz Oct 24 '16 at 19:04
  • @RAZ_Muh_Taz that still brings the 0's as the largest and smallest. Any other thoughts? – John Smth Oct 24 '16 at 19:06
  • 1
    @OrangeDog yup i know of that one i linked the URL, my question is why it gives me 0's as the smallest and largest. – John Smth Oct 24 '16 at 19:07
  • what is the utility of your while loop? – Blip Oct 24 '16 at 19:08
  • @Blip by utility do you mean what is it purpose? – John Smth Oct 24 '16 at 19:12
  • @JohnSmth that is true – Blip Oct 24 '16 at 19:13
  • you could change your large,smalles values into Integer objects, set them to null, in your if checks check if they are null and set them to whatever got entered if they are null – RAZ_Muh_Taz Oct 24 '16 at 19:14
  • There is no connection here between you 'while` and `for-loop`. n is the number of times you are running your `for-loop` but not reading any number. What you are trying to accomplish is get number from user until they enter they want to quit. So nothing is changing in your '`for-loop` it actually never runs because last number is -99 which quits the while loop and condition will be `2<-99` which is false. So it actually never entered the `for-loop`. – thetraveller Oct 24 '16 at 19:15
  • @Niraj Patel can you explain how to remedy this? I'm very new to programming. – John Smth Oct 24 '16 at 19:20
  • @Blip that's to give the user a way to stop inputting numbers and to get the largest/smallest values. – John Smth Oct 24 '16 at 19:27
  • Sure, you don't need `for-loop` dump everything inside the while and use only one user-input instead of two. – thetraveller Oct 24 '16 at 19:30
  • I think you do not understand the flow of the program. First the user is asked how many numbers does he want input in order to compare. Then the program ask the user to input that many number of integers after which it prints the result. Now with your modification, you have created a while loop which is running infinitely till the user inputs -99. So, in the next step the program requires to ask for inputs n number of times. Here the program encounters -99 times. So the program doesn't take any input from the user and prints the variable large and smallest which have been initialised to 0. – Blip Oct 24 '16 at 19:33
  • @John I added the code in the answer. Since this post is being tagged duplicate I can't answer it. – thetraveller Oct 24 '16 at 19:36
  • Duplicate of https://stackoverflow.com/questions/18525474/java-minimum-and-maximum-values-in-array – tripleee Oct 14 '22 at 06:09

1 Answers1

-1
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class LargestAndSmallest {

  public static void main(String... args) {
    final Scanner keyboard = new Scanner(System.in); //init the scanner
    System.out.println("Enter a number");
    final Set<Integer> ints = new HashSet<>(); //init a set to hold user input
    int n; //declare a variable to hold each number
    while ((n = keyboard.nextInt()) != -99) { //loop until 99 is entered
      ints.add(n); //add user input to our set
      System.out.println("Enter more numbers, or -99 to quit.");
    }
    //output aggregate info
    System.out.println("the largest is " + Collections.max(ints)); 
    System.out.println("the smallest is " + Collections.min(ints));
  }
}
Andreas
  • 4,937
  • 2
  • 25
  • 35
  • 1
    While this is correct, perhaps add an explanation? A code dump with no explanation can do more harm than good as the user may implement this with no idea why it works. – basic Oct 24 '16 at 19:13
  • It says the public class must be defined in its own file? – John Smth Oct 24 '16 at 19:16
  • Good point. I added per line comments. Though I think the code is fairly self documenting – Andreas Oct 24 '16 at 19:16
  • Also it says that on the import collections, the declared package does match the excepted package. – John Smth Oct 24 '16 at 19:17
  • @Andreas while the code is self-documenting for a new java developer what may be easy to understand for us to understand is not so easy for early birds. – basic Oct 24 '16 at 19:20
  • @JohnSmth did your original class have a package declaration? Someting like `package edu.compsci201.JohnSmith;`? if so you may need that at the start of the file. @basic is concerned that my answer is unclear, let me know if you have questions. – Andreas Oct 24 '16 at 19:22
  • I Entered it in on the wrong project my fault. Quick little edit, your "the largest is" and your "the smallest is" are flipped around. otherwise code is awesome thank you for the help! – John Smth Oct 24 '16 at 19:25
  • @Andreas the answer became much easier to understand with your comments. Thank you – John Smth Oct 24 '16 at 19:26