Write a method that accepts a String as an argument and returns an int array. Assume that the string contains a series of single digit numbers separated by a comma. (e.g. 2,3,4,5). The method should return an array with the smallest element in the string.Hint: To convert a character to a string use the static toString method in the Character class.

Question
Asked May 8, 2019

Write a method that accepts a String as an argument and returns an int array. Assume that the string contains a series of single digit numbers separated by a comma. (e.g. 2,3,4,5). The method should return an array with the smallest element in the string.

  1. Hint: To convert a character to a string use the static toString method in the Character class.
check_circleExpert Solution
Step 1

Program plan:

  • Import the necessary header files.
  • Define the class “Conversion”.
    • Define the method “small()” to find the smallest element in the string.
      • Initialize the array.
      • Split the string by comma using the method “split()” and store into an array.
      • Use “for” loop to convert the string into an integer array.
      • Use “for” loop to find and return the smallest element in an array.
    • Define the “main()” method.
      • Get the string from the user using “Scanner()” method.
      • Call the method “small()” to find the smallest element.
      • Use “for” loop to print the result.
Step 2

Copyable code:

//Import the necessary header files
import java.util.*;

//Define the class
public class Conversion
{
//Define the method "small"
int[] small(String str)
{
//Initialize the array
int arr[]=null;

//Split the string
String a[]=str.split(",");

//Declare the array length
arr=new int[a.length];

//Loop to find the array length
for(int i=0;i<a.length;i++)

//Convert the string into an array
arr[i]=Integer.parseInt(a[i]);

//Initialize the minimum value
int min=999;

//Initialize the count
int count=0;

//For loop to find the smallest element
for(int i=0;i<arr.length;i++)
{
//Check the condition
if(min>arr[i])

//Assign the minimum value
min=arr[i];

}

//Loop to return the minimum value
for(int i=0;i<arr.length;i++)
{
//Check the condition
if(min==arr[i])

//Increment the count by 1
count++;
}

//Return the minimum value in an array
int r_arr[]=new int[count];
for(int i=0;i<count;i++)
r_arr[i]=min;
return r_arr;
}

//Define the "main()" function
public static void main(String[] args)
{
//Print the statement
System.out.println("Enter the string");

//Create object for "Scanner()" method
Scanner sc = new Scanner(System.in);

//Get the string from the user
String number = sc.nextLine();

//Object creation
Conversion con=new Conversion();

//Call the method "small()"
int a[]=con.small(number);

//Loop to print the result
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}

Step 3

Screenshot of the progra...

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour*

See Solution
*Response times may vary by subject and question
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: 5-27) (Display leap years) Write a program that displays all the leap years, ten per line, from two ...

A: Note: The below program has been complied and executed in NetBeans IDE 8.2

question_answer

Q: In Eclipse write a while loop which prints the even numbers from 20 to 30 (including 30).

A: Program Instructions:Create a class named PrintEven, and initialize a variable i to 20 in the main m...

question_answer

Q: Construct npda that accept the following regular language: L1 = L (aaa∗bab).

A: Non-deterministic pushdown automaton (NPDA):Given: aaa*bab To construct NPDA, when “a” comes then pu...

question_answer

Q: (Method Overloading) Given the following methods, write down the printed output of themethod calls: ...

A: Answer with Explanation:“doSomething(5)” contains an argument integer “5” and it is called to the “d...

question_answer

Q: Write a GUI application that calculates a car’s gas mileage. The application should let the user ent...

A: Create the MPGCalculator class and declare the given fields along with two JPanel gallonsLabel and m...

question_answer

Q: example:"apple-&gt;orange-&gt;banana-&gt;pear"-&gt;NULLreturn "orange"Thanks!

A: ApproachIn the function longest(), we'll declare an integer named highestLength and assign 0 to it.N...

question_answer

Q: Question: Write a program that prompts the user to enter the number of students and each student's s...

A: Solution:The python program has the following algorithm:Prompt and read the total number of students...

question_answer

Q: Create a program that will input the 3 game scores of a player and then output the level of the play...

A: Since there are no messages given in the question for the condition where the weighted average falls...

question_answer

Q: This is a Python question. How do I get the following code to create a numerical list of guest names...

A: We need to convert the guest_name variable as a Python list of 50 elements and initialize each eleme...