-3

I have a java code and it work with success !

//data 
double[] query = {0.65,0.78,0.21,0.29,0.58};

but now i want user put the strings manual like this :

println("insert the values like that {xx,xx,xx,xx,xx} ")

i think must use scanner but i don't know how ?

Aleksandr M
  • 23,647
  • 12
  • 63
  • 129
Mr.Mirou
  • 1
  • 1
  • 2
    Have you looked up some tutorials on using the Scanner class? What did you find confusing? – leigero Apr 17 '17 at 19:01
  • 2
    The official Scanner tutorial: https://docs.oracle.com/javase/tutorial/essential/io/scanning.html You should really talk to your instructor if you don't understand how to use it. – markspace Apr 17 '17 at 19:02
  • i dont know how to pass strings from scanner to array – Mr.Mirou Apr 17 '17 at 19:03
  • 1
    Your instructor really did not explain this? It involves a loop construct (for,while) and an index variable. `i++` is good for the latter. – markspace Apr 17 '17 at 19:05
  • Possible duplicate of [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Kevin J. Chase Apr 17 '17 at 21:49
  • See also: "[Constructing array of double values from user input](https://stackoverflow.com/questions/36660223)". – Kevin J. Chase Apr 17 '17 at 21:51

3 Answers3

0

If you want to get double numbers from user, use nextDouble() method, like this:

Creating a Scanner object:

 Scanner scan = new Scanner(System.in);

Reading inputted numers from user:

Double insertedDouble = scan.nextDouble();
Double insertedDouble2 = scan.nextDouble();

Making an array of inputted numers:

double[] array = {insertedDouble, insertedDouble2};
K.Rzepecka
  • 322
  • 2
  • 9
0

If input is to be taken in from user input, use scanner this way:

Scanner scanner = new Scanner(System.in);

Make sure to intialize array:

private double[] array = new double[10];

Prompt user for input:

System.out.println("Please enter double value: ");
array[0] = scanner.nextDouble(); //gets input from user using scanner, store in first index of array

Print:

System.out.println(array[0]);
Nick Meyer
  • 181
  • 6
0

It's straight forward to use scanner try this:

import java.util.Scanner;
public class Main {
   public static void main(String[] arguments) {
         // Set the number of values
         int values = 5;

         // Create an empty array for values
         double query[] = new double[values];

         // Create scanner object to use
         Scanner input = new Scanner(System.in);


         // Parse Items for input
         for(int i = 0; i < values; ++i) {
             System.out.println("Please input value num in 0.0 format ");
             query[i] = input.nextDouble();
         }

        // Print the values for success
        for(int i = 0; i < values; ++i)
            System.out.println(query[i]);
    }
}
  • Make sure to name the class the same name as the file so in this case the file would be called Main.java. I edited it to make it easier. I just attempted the same thing and it worked. – JoshuaTPritchett Apr 17 '17 at 19:24