Question

In: Computer Science

IN JAVA Write a program with a method that returns an array. The method should accept...

IN JAVA

Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through the array and printing the individual values.

Solutions

Expert Solution

public class ArrayExample {
public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   //reading string
   System.out.println("Enter 3 values separated by comma : ");
   String str=sc.nextLine();
   //calling metho to get array
   int arr[]=getArray(str);
   //printing array
   for(int x:arr)
       System.out.println(x);
}

private static int[] getArray(String arr) {
   int res[]= new int[3];
   int i=0;
   //splitting the array
   for(String s:arr.split(",")){
       try{
       //converting into int a
       res[i++]=Integer.parseInt(s);
       }catch(Exception e){}
   }
   return res;
}
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
In java we will need to create a method that takes the array {1,2,3,4,5} and returns...
In java we will need to create a method that takes the array {1,2,3,4,5} and returns the head reference to a linkedList. We will also need to display the linked list in out main method.
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
java Write a recursive program to reverse a positive integer. . Your method should take a...
java Write a recursive program to reverse a positive integer. . Your method should take a non negative integer as a parameter and return the reverse of the number as an integer. e.g. if you pass 12345, your method should return 54321.
Write a program that uses an array for time and temperature. The program should contain an...
Write a program that uses an array for time and temperature. The program should contain an array with 24 elements, each of which is holding a temperature for a single hour. Your program should have a function that lists all of the temperatures that are held in the array. Temperatures should be listed to console with one entry per line. Your program should have a function that lists a single temperature entry. You should ask the user for a number...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT