-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 ?

6
  • 2
    Have you looked up some tutorials on using the Scanner class? What did you find confusing? Commented Apr 17, 2017 at 19:01
  • 2
    The official Scanner tutorial: 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. Commented Apr 17, 2017 at 19:02
  • i dont know how to pass strings from scanner to array Commented Apr 17, 2017 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. Commented Apr 17, 2017 at 19:05
  • Possible duplicate of Validating input using java.util.Scanner Commented Apr 17, 2017 at 21:49

3 Answers 3

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};
Sign up to request clarification or add additional context in comments.

Comments

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]);

Comments

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]);
    }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.