-1

The purpose of this program is to take in positive integers and once the sentinel value (-1) is hit the program will return the 2 lowest numbers. My program works, but in some scenarios it doesn't. for example if I enter 10,15,20,-1 the program will result in 10 rather than 10 and 15. Here is my program:

public class Small{
public static void main(String [ ] args){
    int number;
    number=IO.readInt();
    int lowest=number; 
    int lowest2=number;

    while (number!=-1){
        number=IO.readInt();
        if(number>0){
        if(number<lowest && number!=-1){
         lowest=number;}
         else{if((number<lowest2||!(number>=lowest2))&& number!=-1){
          lowest2=number;}
        }
    }
    }
IO.outputIntAnswer(lowest);
IO.outputIntAnswer(lowest2);
    }

}

lazepanda
  • 11
  • 4
  • A better solution would to be to make an object that automatically sorts numbers as they are placed in the list, then at the end just retreive the lowest two numbers in your list... – Rabbit Guy Jun 20 '16 at 15:40
  • is there a specific command to find the two lowest numbers? Sorry I'm very new to java – lazepanda Jun 20 '16 at 15:41
  • http://stackoverflow.com/questions/16252269/how-to-sort-a-list-arraylist-in-java – Rabbit Guy Jun 20 '16 at 15:44

3 Answers3

0

The main problem with your code is that you set lowest and lowest2 to the first value you enter. If all the other numbers you enter are higher than the first number, it will never be able to find anything lower.

With your example, the first number is 10 which you assign to lowest and lowest2. When you enter 15, it's not lower than 10, so neither variable can be reset. Same with 20.

You've also got a lot of redundant checks in your if-statements.

public class Small {
  public static void main(String [ ] args) {
    int number;
    int lowest = Integer.MAX_VALUE;
    int lowest2 = Integer.MAX_VALUE;

    while (number!=-1) {
      number=IO.readInt();
      if (number > 0) {
        if(number < lowest) {
          lowest=number;
        }
        else if (number < lowest2) {
          lowest2=number;
        }
      }
    }
  }
  IO.outputIntAnswer(lowest);
  IO.outputIntAnswer(lowest2);
}
Rick
  • 1,863
  • 2
  • 19
  • 46
0

The problem is with your logic flow of the program. Here's what the code is doing:

  1. Read in one number, store in both lowest and lowest2.

  2. Go through loop to read in more numbers

    2a. If the number is positive, check against lowest.

    2a1. If the read in value is lower than lowest, change lowest's value to the read in value.

    2a2. Otherwise, if the read in value is lower than lowest2, change lowest2's value to the read in value. Only do this if lowest was not changed.

    2b. If the read in value is -1, end the loop.

    2c. If the read in value is negative but not -1, continue but don't add the number to lowest or lowest2.

  3. Print out the values for lowest and lowest2.

If you see the error, it's in 2a2. This is linked to the fact that your setup before the while loop is formatted as you did: you made both lowest and lowest2 that first value.

This code would normally run correctly, except for one instance: what if that first value were to be the smallest positive value you entered, with all other values greater than it? lowest2 would be set to that value, and you're checking to see if any other values are smaller than it (which they're not, because they're all going to be greater than it).

Tips for when you're coding:

1) Attempt to develop a logical thinking mentality. Think through how you want to create algorithms for your program. Make sure you don't run into any situations where your program does not run the way you want it to, like in this case (this is called a logic error).

2) Run through your programs by hand when you're finished coding, or after you've run it and got an error. Step through every line of your code, and try to see where and why your code isn't working the way you want it to. Or have someone else see, like a fellow programmer. Sometimes, even the best eyes do not catch their own mistakes.

EDIT: There are various ways to solve this particular problem. One way is to do what Seeker and Rick did: 1) Set both values to the largest possible Integer value, and thus all values entered will be lower than the first values. 2) Ask for the first two inputs at the beginning before the loop, and check them against each other to set the first two lowest values.

However, I believe there are certain things to watch out for when you're doing something like this. These should all be outlined before you code, of course. 1) What if the user is entering only one or zero valid values? Seeker's method wouldn't work, unless he checks that the first/second value is negative/-1. Rick's method wouldn't work either because at least one of the lowest/lowest2 values would still be that max Integer value. 2) What if there were no positive values entered before the -1? This is similar to number 1, and your ints would be null.

You can certainly use Seeker's and Rick's algorithms, but you should check for these conditions if you use them.

ragingasiancoder
  • 616
  • 6
  • 17
  • Could you give a pseudo code example of the logical process I need for this program? I've been trying to do so for a while, but I cannot think of a way. – lazepanda Jun 20 '16 at 18:37
  • I won't give you pseudocode or code, but I did make an edit and expanded on things to watch out for if you decide to use one of the other two pieces of code. I didn't catch all of the conditions that may occur, but you can probably think of some more and come upon them later. – ragingasiancoder Jun 20 '16 at 21:02
-1
import java.util.Scanner;

public class Small {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Number");
        int lowest = scanner.nextInt();
        System.out.println("Enter Number");
        int lowest2 = scanner.nextInt();
        if (lowest > lowest2) {
            int t = lowest;
            lowest = lowest2;
            lowest2 = t;
        }

        while (true) {
            System.out.println("Enter Number");
            int number = scanner.nextInt();
            if (number == -1)
                break;
            if (number < lowest2 && number < lowest) {
                lowest = number;
            } else if (number < lowest2) {
                lowest2 = number;
            }
        }
        System.out.println("lowest:" + lowest);
        System.out.println("lowest2:" + lowest2);
        scanner.close();
    }
}
Prateek Kapoor
  • 947
  • 9
  • 18