scan.nextLine() only allowing strings and not doubles in Java

I am trying to get my Java program to accept and then print two double primitives in reverse order. However, I keep getting the error:

String can not be converted to a double

image

My code:


/* Lesson 3 Coding Activity Question 1 */

import java.util.Scanner;

class U1_L3_Activity_One {
  public static void main(String[] args) {
      
    Scanner scan = new Scanner(System.in);
    
    double double1;
    double double2;
    
    double1 = scan.nextLine();
    double2 = scan.nextLine();
    
    System.out.println(double2 +" "+ double1);
  }
}

I have no idea why this is happening.

You can parse the input to a double using Double.parseDouble(scan.nextLine()).

1 Like

Also just figured out you can use scan.nextDouble();

1 Like