Hello! I'm new to these boards and I have long stalked them for help, and I figured it was time to finally join in on the fun. I started with an intro to C class and did well in it. This is my second programming class and introduction to OOP via Java.
My assignment is to find the two smallest integers entered. No arrays may be used. Any valid integers will be entered, numbers may be repeated, and no need to handle non-numeric input. They will only test for positive and negative integers. I have to also use EOF as the first character sequence on a line to indicate no more input, and the code has to handle any number of entries including less than 2.
Here are some sample executions...
Enter a series of integers; EOF to quit.
-16 3 -16
^D
Smallest distinct number... -16
Second smallest number ... 5
Enter a series of integers; EOF to quit.
5
^D
Smallest distinct number... 5
Enter a series of integers; EOF to quit.
4 0
5 -20
34
22
-1
^D
Smallest distinct number... -20
Second smallest number ... -1
Enter a series of integers; EOF to quit.
3 3
^D
Smallest distinct number... 3
Enter a series of integers; EOF to quit.
^D
No number entered.
Anyhow this is my code so far, and it makes sense to me however I simply cannot get it to run without a bunch of errors. What I'm trying to do is have input.nextInt() read off how-ever many numbers were inputted, and comparing them via my if else statements. By doing so I will have the two smallest integers in the end and will print them individually. I really do apologize if it is messy, but this is my second day on this and my brain has officially run out of possibilities for this code to run. I feel like I'm so close!! Any help would be great!
My assignment is to find the two smallest integers entered. No arrays may be used. Any valid integers will be entered, numbers may be repeated, and no need to handle non-numeric input. They will only test for positive and negative integers. I have to also use EOF as the first character sequence on a line to indicate no more input, and the code has to handle any number of entries including less than 2.
Here are some sample executions...
Enter a series of integers; EOF to quit.
-16 3 -16
^D
Smallest distinct number... -16
Second smallest number ... 5
Enter a series of integers; EOF to quit.
5
^D
Smallest distinct number... 5
Enter a series of integers; EOF to quit.
4 0
5 -20
34
22
-1
^D
Smallest distinct number... -20
Second smallest number ... -1
Enter a series of integers; EOF to quit.
3 3
^D
Smallest distinct number... 3
Enter a series of integers; EOF to quit.
^D
No number entered.
Anyhow this is my code so far, and it makes sense to me however I simply cannot get it to run without a bunch of errors. What I'm trying to do is have input.nextInt() read off how-ever many numbers were inputted, and comparing them via my if else statements. By doing so I will have the two smallest integers in the end and will print them individually. I really do apologize if it is messy, but this is my second day on this and my brain has officially run out of possibilities for this code to run. I feel like I'm so close!! Any help would be great!
import java.util.*; public class SmallestTwo { public static void main(String[] args) { int min1 = 0; int min2 = 0; int input = 0; Scanner input = new Scanner (System.in); System.out.print ("Enter a series of integers; EOF to Quit."); while ( input.hasNext() ) { number = input.nextInt(); min1 = num1; min2 = num1; if (num2 <= min1) { min2 = min1; min1 = num2; } else if (num2 < min2) { min2 = num2; } else if (min1 == min2) { min2 = num2; } System.out.println("Smallest distinct number entered was " + min1); System.out.println("Second smallest distinct number entered was " + min2); } } }