Write your code in the file TwoSmallestjava We wish to write
Write your code in the file TwoSmallest.java. We wish to write a program that takes a set of numbers and determines which are the two smallest. Ask the user for the following information, in this order: A terminating value (real number). The user will enter this value again later, to indicate that he or she is finished providing input. A sequence of real numbers. Keep asking for numbers until the terminating value is entered. Compute and output the smallest and second-smallest real number, in that order. It is possible for the smallest and second-smallest numbers to be the same (if the sequence contains duplicate numbers).
Example
In ALL cases of error on input, RE-ASK the user for the input until it is correctly entered.
Solution
Code:
//TwoSmallest.java
import java.io.*;
import java.lang.*;
import java.util.*;
public class TwoSmallest
{
//main
public static void main(String[] args)
{
//Scanner object
Scanner mySan=new Scanner(System.in);
//Declare term
float term;
//Get the terminating value
while(true)
{
//Get term
term=mySan.nextFloat();
//Check user has entered terminating value. If not, print error-msg
if(term!=123)
{
//printing error-msg
System.out.println(\"Enter the terminating value to start the process\");
}
//user has entered terminating value
else
//exit from loop
break;
}
//Declare previous and current small element
float prevSmall=term;
float currSmall=term;
//Loop to get set of nos.
while(true)
{
//Get no
float no=mySan.nextFloat();
//Check if user has entered terminating-value
if(no==123)
//If so, exit from loop
break;
//Otherwise, check if no is lemySan than current smallest element
if(no<currSmall)
{
//Assign currSmall to prevSmall
prevSmall=currSmall;
//Set currSmall to no
currSmall=no;
}
}
//Print currSmall and prevSmall
System.out.println(\"RESULT: \"+ currSmall);
System.out.println(\"RESULT: \"+ prevSmall);
}
}
Sample output:
123
17
23.5
15.2
10
30
8
16
123
RESULT: 8.0
RESULT: 10.0


