2

I am using NetBeans IDE 7.4 and I am very new to Java and I am trying to get the user to input two numbers (both of them are doubles) and then store them in another variable. I have gone to DOZENS of tutorial sites and they are no good. How can I get basic input and outputs?

2 Answers 2

4

Primitive example:

import java.util.Scanner;

public class TestIO {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Print something:");    // printing output
        String text = scanner.nextLine();          // taking input

        System.out.println("You have printed the following text: " + text);
    }

}


UPDATE:
Sorry, missed the point, that you want to take doubles. Here you go:

import java.util.Scanner;
import java.util.InputMismatchException;

public class TestIO {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double firstDouble = 0L;
        double secondDouble = 0L;
        while (true) {  // take input (two doubles) until successful
            System.out.println("Print two doubles (press enter after each input):");
            try {
                firstDouble = scanner.nextDouble();
                secondDouble = scanner.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("Wrong input. You must print doubles.\n" +
                                   "Depending on your locale, as a decimal separator " +
                                   "use either a comma or a dot.\n");
                scanner.nextLine(); // clearing the buffer with the wrong input
                                    // to avoid infinite loop
                continue;
            }
            break; 
        }
        // Printing the user input (limiting doubles to 3 decimal places)
        System.out.format("You have printed %.3f and %.3f %n", 
                          firstDouble, secondDouble);
    }
}

Recommended reading:

Sign up to request clarification or add additional context in comments.

2 Comments

double d1 = Double.parseDouble(text);
@sgmart Yeah, there're lots of variations. I just threw some basic example ))
0
import java.util.*;

public class Test
{
static Scanner console = new Scanner (System.in)
public static void main (String[] args)
{
double input1, input2; // Declares the double value of input1 and input2
input1 = console.nextDouble(); // User inputs value into input1
input2 = console.nextDouble();

String value1 = "" + input1; // Storing value into a String
String value2 = "" + input2;
// If you want to store it in let's say string.
// Or else i think / int value1 = Integer.parseInt(input1);
// This changes the double value into an Int value
// Something like that.
}
}

Not quite sure if that was your question because outputting a value could also be with

System.out.println(input1); // This outputs the value of input1 on the screen.

Comments

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.