This is an intro to Java Question. My current solution is giving
me bad outputs. Please show me your way of solving this.
Problem 4: Min/Max Search by Value Develop a program that, given a
sequence S of integers as input, produces as two output values, the
first is the minimum value that appears in the sequence and the
second is the maximum value that appears in the sequence.
Facts
● Scanner has a method that returns a boolean indicating whether a next integer exists in its input stream ( hasNextInt() )
● Scanner objects can be initialized to to scan String data as input.
Input
The input will begin with a single line containing T , the number of test cases to follow. The remaining lines contain the T sequences, one line per sequence. Each of these lines contains the values in the sequence. Each such value is separated from the next by at least one space.
Output For each sequence given as input, there should be four lines of output. The first line echos the given sequence. The second line indicates the minimum value that occurs. The third line indicates the maximum value that occurs. The fourth line is blank.
Sample Input
3
3 6 -1 4 6 5 3
0 0 0 0 -4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3
Sample Output 3 6 -1 4 6 5 3
-1
6
0 0 0 0
0
0
-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3
-7
854
So Far This is what I have:
import java.util.Scanner;
public class MinMaxSearchByValue {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int cases = input.nextInt();
input.nextLine();
for (int i = 0; i < cases; i++) {
String givenListOfNums = input.nextLine();
Scanner stringScanner = new Scanner (givenListOfNums);
int min = stringScanner.nextint();
int max = min;
while (stringScanner.hasNextInt()) {
int nextValue = stringScanner.nextInt();
if(nextValue < min) {
min = nextValue;}
else if (nextValue > max)
{
max =
nextValue;
}
}
}
System.out.printf("%s%n%d%n%d%n%n", givenListOfNums, min, max);
}
}
import java.util.Scanner;
public class MinMaxSearchByValue {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
System.out.println("Enter number of
commas: ");
//reading the number of
cases
int cases = input.nextInt();
input.nextLine();
for (int i = 0; i < cases;
i++) {
//reading
elements from user
System.out.println("Enter elements separeted by comma: ");
String
givenListOfNums = input.nextLine();
//splitting the
array with , as delimeter
String
arr[]=givenListOfNums.split(",");
//assuming
1st element as min and max
int min =
Integer.parseInt(arr[0]);
int max = min;
for(int j=1;j<arr.length;j++){
//comparing min value with current value
int nextValue = Integer.parseInt(arr[j]);
// if current value is small than making current
value as min
if (nextValue < min) {
min = nextValue;
}
// if current value is max than making current
value as max
else if (nextValue > max) {
max = nextValue;
}
}
System.out.printf("Elements :%s%nMin element:%d%nMax element :
%d%n%n", givenListOfNums, min, max);
}
}
}
Note : If you like my answer please rate and help me it is very Imp for me
Get Answers For Free
Most questions answered within 1 hours.