Scanner scan = new Scanner(System.in);
double numbers = scan.nextDouble();
double[] avg =..????
Asked
Active
Viewed 3.9e+01k times
19
-
is it a known or an unknown number of entries? – Bozho May 08 '10 at 19:45
11 Answers
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
![](../../users/profiles/243943.webp)
npinti
- 50,175
- 5
- 67
- 92
-
This won't work when you go over the array capacity (5). A better option would be to use a List. – Mark Hughes Oct 07 '16 at 06:37
-
-
thats why I left it as a comment not an answer. These questions commonly appear in search results :) – Mark Hughes Oct 10 '16 at 05:41
8
You can get all the doubles with this code:
List<Double> numbers = new ArrayList<Double>();
while (scan.hasNextDouble()) {
numbers.add(scan.nextDouble());
}
![](../../users/profiles/335974.webp)
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);
}
}
![](../../users/profiles/1358283.webp)
kratenko
- 6,652
- 4
- 32
- 57
![](../../users/profiles/1494903.webp)
Jossel M. Barnobal
- 31
- 2
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+" ,");
}
}
![](../../users/profiles/6191407.webp)
Sonu patel
- 351
- 1
- 8
-
2Code 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");
}
}
}
![](../../users/profiles/1532460.webp)
awesoon
- 26,766
- 9
- 62
- 86
![](../../users/profiles/2346293.webp)
ASHISH RANJAN
- 61
- 10
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();
![](../../users/profiles/3596851.webp)
Harsha pps
- 1,533
- 2
- 20
- 32
0
double [] avg = new double[5];
for(int i=0; i<5; i++)
avg[i] = scan.nextDouble();
![](../../users/profiles/297167.webp)
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);
}
}
![](../../users/profiles/4483806.webp)
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();
}
}
![](../../users/profiles/9222996.webp)
CKE
- 1,344
- 9
- 14
- 25
![](../../users/profiles/11747105.webp)
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);
}
}
![](../../users/profiles/908174.webp)
Luís Cruz
- 13,666
- 16
- 64
- 89
-
1While 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