Question

Write a Java program that reads a 1D array of integers from standard input and returns...

Write a Java program that reads a 1D array of integers from standard input and returns it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//TestCode.java
import java.util.Scanner;
public  class TestCode {
    public static int[] readArray(){
        int size;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter size of array: ");
        size = scan.nextInt();

        int arr[] = new int[size];
        System.out.println("Enter "+size+" values for array");
        for(int i = 0;i<size;i++){
            arr[i] = scan.nextInt();
        }
        return arr;
    }

    public static void main(String[] args) {
        int arr[] = readArray();
        System.out.println("\nArray is:");
        for(int i = 0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

Output:

Enter size of array: 5 Enter 5 values for array 6 3 5 4 2 Array is: 6 3 5 4 2 Process finished with exit code 0

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a Java program that reads a 1D array of integers from standard input and returns...
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