Storing doubles in an array

Ok, so I want to store the values ​​in an array. Here is my code:

import java.util.Scanner;
public class test {
    public static void main(String args[]){
        Scanner bob = new Scanner(System.in);

        double[] mylist;

        System.out.println("What is your first value?");
        mylist = bob.nextDouble();

        System.out.println("What is your second value?");
        mylist = bob.nextDouble();
    }
}

      

Problem # 1: They tell me what it mylist = bob.nextDouble()

should be nextLine()

. Why is this if it is clearly double?

Purpose. I want to somehow store these values ​​given in an array so that I can then pull them out. Can anyone please help?

UPDATE

In issue # 1, he informs me that he cannot convert from Double()

to Double[]

. What does it mean?

+3


source to share


5 answers


mylist

is array

twins, not one double

.

When you do mylist = bob.nextDouble();

, you are basically trying to make the entire array equal to one number.



Instead, you need to assign a number to a specific location in the array.

  • Initialize the array with the number of elements you intend to store in the array:

    double[] mylist = new double[2];
    
          

  • Assign a double to a specific location in the array:

    mylist[0] = bob.nextDouble();
    mylist[1] = bob.nextDouble();
    
          

+3


source


The error is when you are trying to assign a double value to an array in the following expression:

    mylist = bob.nextDouble();

      

since mylist is defined as an array here:

    double[] mylist;

      



First you need to inject your array:

    double[] mylist = new double[2]; // initialise array to have two elements

      

and then try to assign the elements of the array with user input like this:

    mylist[0] = bob.nextDouble();
    mylist[1] = bob.nextDouble();

      

+5


source


It is an array (which is the type of object). First, create an array large enough to hold double

(s) (not int

(s)). And store the values ​​at the appropriate indices. Something like,

Scanner bob = new Scanner(System.in);
double[] mylist = new double[2];
System.out.println("What is your first value?");
mylist[0] = bob.nextDouble();
System.out.println("What is your second value?");
mylist[1] = bob.nextDouble();
System.out.println(Arrays.toString(mylist));

      

+3


source


The array is not double, you need to select the index:

double[] arr = new double[2];
System.out.println("What is your first value?");
arr[0] = bob.nextDouble();

System.out.println("What is your second value?");
arr[1] = bob.nextDouble();

      

Also, don't call the array in a list, use CamelCase to name your variables / methods / classes and initialize the array. I recommend that you read the Java tutorial for working with the basics of the language.

+3


source


Better yet, use a generic list.

List<Double> myList;

myList.add(bob.nextDouble());

      

This way you can add as many pairs as you want to the list. So you don't have to worry about indexing.

0


source







All Articles