-6

I want a program that asks first for the size of an array and next asks for the element. The size of the array is int and the stored values are double. Like this:

How many numbers? 5 // 5 is user input
Please type the numbers:
1,111
4
11,45
21
3
The numbers in reverse order are:
3.0 21.0 11.45 4.0 1.111

My problem is, how to ask for the size and for the elements? Thanks in advance!

  • 3
    System.in.read() is your friend – aran Feb 28 '19 at 08:38
  • You can also take a look at the Scanner class..Though System.in.read() would also do just fine – akshaya pandey Feb 28 '19 at 08:39
  • Scanner sc = new Scanner(System.in); int size=sc.nextInt(); – Akash Shah Feb 28 '19 at 08:39
  • 2
    Possible duplicate of [How to read integer value from the standard input in Java](https://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java) – Beast77 Feb 28 '19 at 08:40
  • On some systems `Console console = System.console();` is also supported and makes your life easier – Lino Feb 28 '19 at 08:54
  • I know the Scanner method. Like this: Int a = sc.nextInt(); I can do with arrays the same way?? Int [] arrayNumbers = sc.nextInt(); – Tibor Tóth Feb 28 '19 at 08:55
  • @TiborTóth If you know how to input an integer then you can use it to get the size of the array. Then initialise an array of that size. Then loop "size" number of times and in each iteration again input an integer and set at the corresponding array index.(This is using scanner class) – uneq95 Feb 28 '19 at 09:20

1 Answers1

0

Use below code.

Scanner sc = new Scanner(System.in); 
int arraysize=sc.nextInt();
double[] doubleArray = new double[arraysize];

for(int i=0; i<arraysize; i++){
doubleArray[i] = sc.nextDouble(); 

}

Onkar Musale
  • 765
  • 9
  • 22