Question

This is an intro to Java Question. My current solution is giving me bad outputs. Please...

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);

   }


}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hello can someone show me how to incorporate my method integerPower into the main? I want...
Hello can someone show me how to incorporate my method integerPower into the main? I want it to be able to take in the values the user inputs and calculate the value. here is the code I have so far: { public static void main(String[] args) { Scanner input = new Scanner(System.in); int base; int exponent; System.out.printf("Enter base value: ");    base = input.nextInt();    System.out.printf("Enter the exponent value:");    exponent = input.nextInt(); } public static int integerPower(int base, int...
This is an intro to Java question. Please solve using pseudocode if you can. Fibonacci ​...
This is an intro to Java question. Please solve using pseudocode if you can. Fibonacci ​ In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... Hence, a Fibonacci number ​n ​ may be found with ​f(n) = f(n-1)...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers...
Intro to JAVA Problem 1: Summing It Up Write a program, which takes two distinct integers separated by space as input and prints the sum of all the integers between them, including the two given numbers. Note that the numbers can appear in either order. You may assume that both numbers are between –10, 000 and 10, 000. For example, if the input is as follows: 10 4 the output should be 49, since 10+9+8+7+6+5+4=49. Similarly, if the input is...
This is an intro to Java question. Please include pseudo code for better understanding. I am...
This is an intro to Java question. Please include pseudo code for better understanding. I am hoping to use this to do some reviewing. We are mainly focusing on input and output declarations and invoking API methods. Problem 6: Log It (10 points) Use API (Data Structure Algorithms) High-Low is a simple number guessing game where one player thinks of a random integer number between 0 to some maximum value and another player attempts to guess that number. With each...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth...
IN JAVA In this problem, we will implement an nth root finder. Recall that the nth root of x is the number when raised to the power n gives x. In particular, please fill in the method findNthRoot(int number, int n, int precision) within the Main class. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should still fill in the answer with decimal...
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
In this problem, we will implement an nth root finder. In particular, please fill in the...
In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may not use any library functions for this...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT