Question

Write program in JAVA with a method that returns an array. The method should accept as input a co...

Write program in JAVA 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 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. Submit your source code and screenshots of the application executing the application and the results in a single document.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class CommaSeparatedArray {

    public static String[] splitString(String s) {
        String[] words = s.split(",");
        for (int i = 0; i < words.length; i++) {
            words[i] = words[i].trim();
        }
        System.out.println("success");
        return words;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a command separated line: ");
        String[] words = splitString(in.nextLine());
        System.out.println("Words are");
        for (int i = 0; i < words.length; i++) {
            System.out.println(words[i]);
        }
        in.close();
    }
}

separated line: suCCess Words are how you?

separated line: suCCess Words are how you?

Add a comment
Know the answer?
Add Answer to:
Write program in JAVA with a method that returns an array. The method should accept as input a co...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT