JiggyJager
JiggyJager

Reputation: 1

Java program to find the second smallest number in console input

I am really trying to grasp this problem but could not continue further without help. My logic is in order but for some reason it will not execute properly.

On execution I want to enter a line of numbers for example 10 12 5 9 3 and the program should return me the second smallest number. As I want to control the basics first I am refraining from using any other imported classes except for the two used.

If someone could shed some light on why this does not work I would be very grateful.

package secondSmallest;

import java.util.Scanner;
import java.io.PrintStream;

public class secondSmallest {

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    PrintStream out = new PrintStream(System.out);
    
    int smallest = 1;
    int secsmallest = 0;
    int hold = 0;
    
    while(scan.hasNext()); {
        
        hold = scan.nextInt();
        
        if (hold < smallest) {
            smallest = secsmallest;
            smallest = hold;
            
            
        } else {
            hold = scan.nextInt();
        }
        
    out.printf(" %d", secsmallest);
    }
    }
}

Upvotes: 0

Views: 877

Answers (2)

Giorgi Tsiklauri
Giorgi Tsiklauri

Reputation: 11136

First of all:

My logic is in order but for some reason it will not execute properly.

means that your logic is not in order (unless there is just a typo, or other syntax error, blocking flawless result);


Second:

Scanner#hasNext(), which you have as a while condition:

Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input.

and you should, somewhere, somehow, indicate when you want your while loop to end. In your example, your loop is going on infinitely, as it does not have any base case. Even the "Enter" keystroke is a data, and pressing it will keep entering new-line control character;


Third:

You have your smallest initialized as 1, which is not really a clean design to statically assign the constant to the current minimum. Think of a possibility when your input is different;


Fourth:

You are printing the secsmallest inside your while loop, which, I suppose, is not what you meant to do;


Fifth:

By reading in your else block hold = scan.nextInt(); you are effectively ommiting one input, as the moment your while iterates one step forward, you have another hold = scan.nextInt(); and you jump one iteration;


Sixth:

There are many ways to design "find-second-smallest" algorithm (sorting it first and then taking the second element; introducing two pointers; etc.), but if you insist to follow something close to your way, this works as expected:

public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 12, 5, 9, 32, 5, 123, 4, -34, 12, -534, -53, -1, 432, 53};
        int res = secondSmallest(arr);
        System.out.println(res);
    }

    public static int secondSmallest(int[] arr) {
        int smallest = arr[0];
        int secsmallest = arr[1];
        int i = 2;
        while (i < arr.length-1) {
            int current = arr[i];
            if (current < smallest) {
                 secsmallest = smallest;
                 smallest = current;
            }
            else if(current < secsmallest) {
                secsmallest = current;
            }
            i++;
        }
        return secsmallest;
    }
}

Outputting:

-53

Upvotes: 2

ArmDuke
ArmDuke

Reputation: 108

Your program should look like this

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    PrintStream out = new PrintStream(System.out);
    int smallest = Integer.MAX_VALUE;
    int secSmallest = smallest;
    int hold = 0;
    while (scan.hasNextInt()) {
        hold = scan.nextInt();
        if (hold < smallest) {
            secSmallest = smallest;
            smallest = hold;
        } else if (hold < secSmallest) secSmallest = hold;
    }
    out.printf(" %d", secSmallest);
}

Upvotes: -1

Related Questions