19
Scanner scan = new Scanner(System.in);
double numbers = scan.nextDouble();
double[] avg =..????
Radiodef
  • 35,285
  • 14
  • 78
  • 114
WM.
  • 2,629
  • 8
  • 29
  • 34

11 Answers11

23

You could try something like this:

public static void main (String[] args)
{
    Scanner input = new Scanner(System.in);
    double[] numbers = new double[5];

    for (int i = 0; i < numbers.length; i++)
    {
        System.out.println("Please enter number");
        numbers[i] = input.nextDouble();
    }
}

It seems pretty basic stuff unless I am misunderstanding you

npinti
  • 50,175
  • 5
  • 67
  • 92
8

You can get all the doubles with this code:

List<Double> numbers = new ArrayList<Double>();
while (scan.hasNextDouble()) {
    numbers.add(scan.nextDouble());
}
Felipe Cypriano
  • 2,697
  • 20
  • 32
  • how do we add Strings to the `List`, I used this code `while(scanner.hasNext()){ list.add(scanner.next().toString());}` but it gives me `NullPointerException` – Kasun Siyambalapitiya Jun 02 '16 at 17:15
3
import java.util.Scanner;

public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner in=new Scanner (System.in);
        int num[]=new int[10];
        int average=0;
        int i=0;
        int sum=0;

        for (i=0;i<num.length;i++) {
            System.out.println("enter a number");
            num[i]=in.nextInt();
            sum=sum+num[i];
        }
        average=sum/10;
        System.out.println("Average="+average);
    }
}
kratenko
  • 6,652
  • 4
  • 32
  • 57
3
**Simple solution**
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int size;
    System.out.println("Enter the number of size of array");
    size = sc.nextInt();
    int[] a = new int[size];
    System.out.println("Enter the array element");
    //For reading the element
    for(int i=0;i<size;i++) {
        a[i] = sc.nextInt();
    }
    //For print the array element
    for(int i : a) {
        System.out.print(i+" ,");
    }
}
Sonu patel
  • 351
  • 1
  • 8
  • 2
    Code is always good, but it also helps to provide some comments about your solution solves the original question. This is a suggestion for further improving this and future answers. – craigcaulfield Jun 08 '18 at 06:01
  • Thanks man ,so write now its better or some on other formating issue again thanks a lot. – Sonu patel Jun 09 '18 at 14:37
1
import  java.util.Scanner;

class Array {
public static void main(String a[]){

    Scanner input = new Scanner(System.in);

    System.out.println("Enter the size of an Array");

    int num = input.nextInt();

    System.out.println("Enter the Element "+num+" of an Array");

    double[] numbers = new double[num];

    for (int i = 0; i < numbers.length; i++)
    {

        System.out.println("Please enter number");

        numbers[i] = input.nextDouble();

    }

    for (int i = 0; i < numbers.length; i++)
    {

        if ( (i%3) !=0){

            System.out.print("");

            System.out.print(numbers[i]+"\t");

        } else {
            System.out.println("");

            System.out.print(numbers[i]+"\t");
        }

    }

}
awesoon
  • 26,766
  • 9
  • 62
  • 86
1
List<Double> numbers = new ArrayList<Double>();
double sum = 0;

Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
    double value = scan.nextDouble();
    numbers.add(value);
    sum += value;
}

double average = sum / numbers.size();
Harsha pps
  • 1,533
  • 2
  • 20
  • 32
0
double [] avg = new double[5];
for(int i=0; i<5; i++)
   avg[i] = scan.nextDouble();
Feyyaz
  • 3,067
  • 4
  • 33
  • 50
0

This is a program to show how to give input from system and also calculate sum at each level and average.

package NumericTest;

import java.util.Scanner;

public class SumAvg {


 public static void main(String[] args) {

 int i,n;
 System.out.println("Enter the number of inputs");
 Scanner sc = new Scanner(System.in);
 n=sc.nextInt();
 int a[] = new int [n];

    System.out.println("Enter the inputs");
   for(i=0;i<n;i++){
   a[i] = sc.nextInt();
  System.out.println("Inputs are " +a[i]);
 }

  int sum = 0;
  for(i=0;i<n;i++){
 sum = sum +a[i];
  System.out.println("Sums : " +sum);
 }
  int avg ;
  avg = sum/n;
  System.out.println("avg : " +avg);
  }
 }
UDID
  • 2,230
  • 3
  • 14
  • 31
0
public static void main (String[] args)
{
    Scanner s = new Scanner(System.in);
    System.out.println("Please enter size of an array");
    int n=s.nextInt();
    double arr[] = new double[n];
    System.out.println("Please enter elements of array:");
    for (int i=0; i<n; i++)
    {
        arr[i] = s.nextDouble();
    }
}
CKE
  • 1,344
  • 9
  • 14
  • 25
Rohit Mittal
  • 60
  • 1
  • 7
-1
import java.util.Scanner;
public class sort {

  public static void main(String args[])
    {
        int i,n,t;          

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of array");

        n=sc.nextInt();

        int a[] = new int[n];

        System.out.println("Enter elements in array");

        for(i=0;i<n;i++)
        {
            a[i]=sc.nextInt();
        }
        t=a[1];

        for(i=0;i<n;i++)
        {
            if(a[i]>t)

                t=a[i];
        }
        System.out.println("Greates integer is" +t);
    }
}
Luís Cruz
  • 13,666
  • 16
  • 64
  • 89
  • 1
    While your answer may solve the question, it is always better if you can provide a description of what the issue was and how your answer solves it. This is a suggestion for further improving this and future answers. – Luís Cruz Nov 22 '14 at 15:54
-1
Scanner scan = new Scanner (System.in);

for (int i=0; i<=4, i++){

    System.out.printf("Enter value at index"+i+" :");

    anArray[i]=scan.nextInt();

}
Dennis
  • 30,518
  • 9
  • 59
  • 75