Incorrect output when finding second smallest integer

user2979713

The following code only occasionally returns the right value, and it seems completely random when it does and when it doesn't. Pointers please?

/*This program takes a series of integers as user input
 * and returns the second smallest amongst them to the
 * console. 
 * 
 * For reasons unknown to the programmer, ending user 
 * input with Ctrl-D only works occasionally. If Ctrl-D
 * fails, enter any character(s) to the console and press 
 * enter. Make sure the dummy characters are separated 
 * from any integers with whitespace. The program should 
 * execute normally, ignoring the dummy character(s).
 * 
 * Written by xxxxxx, w45-2013*/

package secondSmallest;

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

public class SecondSmallest {

    PrintStream out;
    Scanner in;

    SecondSmallest() {
        out = new PrintStream(System.out);
        in = new Scanner(System.in);
    }

    void start() {
        out.printf("Enter 3 or more positive integers, seperate with whitespace. End input with Ctrl-D.%nInteger input:");
        int smallest, secondSmallest = 2147483647, nextInt;
        smallest = in.nextInt();
        while (in.hasNextInt()) {
            nextInt = in.nextInt();
            if (nextInt < smallest) {
                secondSmallest = smallest;
                smallest = nextInt;
            }
        }
        out.printf("The second smallest integer is %d", secondSmallest);
    }

    public static void main(String[] args) {
        new SecondSmallest().start();
    }
}
Zong

It could be that you're missing the second case for when only the second smallest number needs to be updated:

if (nextInt < smallest) {
    secondSmallest = smallest;
    smallest = nextInt;
} else if (nextInt < secondSmallest) {
    secondSmallest = nextInt;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the second smallest integer in array

From Dev

Second smallest number in array - incorrect output

From Dev

finding smallest and second smallest number

From Dev

Finding the smallest integer

From Dev

Finding the smallest and second smallest value in an array Java

From Dev

Finding Second Smallest Element in Array

From Dev

Finding the k smallest odd integer

From Dev

Finding the k smallest odd integer

From Dev

Finding the second smallest number using loops in python

From Dev

finding smallest and second smallest value of an array in C++

From Dev

Finding subset sum recursively producing incorrect output

From Dev

java binary search tree finding second smallest node

From Dev

java binary search tree finding second smallest node

From Dev

Incorrect output when indexing into a string

From Dev

Incorrect output when using sizeof()

From Dev

Negative numbers not considered when finding smallest number in an array

From Dev

Finding the second smallest number from the given list using divide-and-conquer

From Dev

Finding smallest prime factor

From Dev

finding smallest value in PostgreSQL

From Dev

Finding smallest multiple in ruby

From Dev

Evaluating 0 when trying to find the smallest integer fails

From Java

The output value is a big integer that is incorrect and doesn't change with changing input

From Dev

Incorrect output format when translating DNA to Protein

From Dev

Incorrect output when downloading .html files

From Dev

Why is my output incorrect when in base 16

From Dev

Incorrect positioning of the image when output to the screen

From Dev

incorrect c++ output when executing

From Dev

Second smallest element in a list

From Dev

Finding smallest value in generic ArrayList

Related Related

  1. 1

    Finding the second smallest integer in array

  2. 2

    Second smallest number in array - incorrect output

  3. 3

    finding smallest and second smallest number

  4. 4

    Finding the smallest integer

  5. 5

    Finding the smallest and second smallest value in an array Java

  6. 6

    Finding Second Smallest Element in Array

  7. 7

    Finding the k smallest odd integer

  8. 8

    Finding the k smallest odd integer

  9. 9

    Finding the second smallest number using loops in python

  10. 10

    finding smallest and second smallest value of an array in C++

  11. 11

    Finding subset sum recursively producing incorrect output

  12. 12

    java binary search tree finding second smallest node

  13. 13

    java binary search tree finding second smallest node

  14. 14

    Incorrect output when indexing into a string

  15. 15

    Incorrect output when using sizeof()

  16. 16

    Negative numbers not considered when finding smallest number in an array

  17. 17

    Finding the second smallest number from the given list using divide-and-conquer

  18. 18

    Finding smallest prime factor

  19. 19

    finding smallest value in PostgreSQL

  20. 20

    Finding smallest multiple in ruby

  21. 21

    Evaluating 0 when trying to find the smallest integer fails

  22. 22

    The output value is a big integer that is incorrect and doesn't change with changing input

  23. 23

    Incorrect output format when translating DNA to Protein

  24. 24

    Incorrect output when downloading .html files

  25. 25

    Why is my output incorrect when in base 16

  26. 26

    Incorrect positioning of the image when output to the screen

  27. 27

    incorrect c++ output when executing

  28. 28

    Second smallest element in a list

  29. 29

    Finding smallest value in generic ArrayList

HotTag

Archive