-2
error: incompatible types: String cannot be converted to double[]
      polyval = usrIn.nextLine();
                              ^
1 error

Why is this not working? what am I doing wrong?

import java.util.Scanner; 

public class MainApp
{
   public static void main(String[] args){

      Scanner usrIn = new Scanner(System.in);
      double aval, bval, cval;

      System.out.println("Please enter infromation for three polynomials of type ax^2+bx+c.");

      System.out.println("Enter a, b, and c for polynomial 1:");

      double[] polyval = new double[3];
      polyval = usrIn.nextLine();

      System.out.println("Enter a, b, and c for polynomial 2:");
      double[] polyval2 = new double[3];
      //polyval2 = usrIn.nextLine();

      System.out.println("Enter a, b, and c for polynomial 3:");
      double[] polyval3 = new double[3];
      //polyval3 = usrIn.nextLine();
      System.out.println("Enter a, b, and c for polynomial 3:"
                         +"\t1. Print All Polynomials"
                         +"\t2.  Add Two Polynomials"
                         +"\t3. Factor Using Quadratic Formula"
                         +"\t4. Update a Polynomial"
                         +"\t5. Find Largest Polynomial"
                         +"\t6. Enter X and Solve"
                         +"\t7. Clear All Polynomials"
                         +"\t8. Exit the Program");
     }

}
  double[] polyval = new double[3];
  polyval = usrIn.nextLine();

How do I fix this

 error: incompatible types: String cannot be converted to double[]         
 polyval = usrIn.nextLine();
                             ^    
1 error
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • 4
    What do you think `nextLine()` returns? Did you read the documentation? – GBlodgett May 23 '19 at 16:05
  • 1
    The error message says it all. You are trying to assign a string to a double. You'll need to convert it. – lurker May 23 '19 at 16:07
  • 1
    Possible duplicate of [convert string into int or double?](https://stackoverflow.com/questions/13853181/convert-string-into-int-or-double) – Stefan Freitag May 23 '19 at 16:14
  • Possible duplicate of [Convert String to double in Java](https://stackoverflow.com/questions/5769669/convert-string-to-double-in-java) – Boyan Hristov May 23 '19 at 18:02

1 Answers1

0

usrIn.nextLine() will return a String. If you want to convert that into a double[], you will need to first parse the string into an array by using split presumably on a space (assuming that is how you are inputing). Then for each String representation of the number, you will have to convert it into a double

    double[] polyval = new double[3];
    String[] nextLine = usrIn.nextLine().split(" ");

    for (int i = 0; i < nextLine.length; i++) {
      polyval[i] = Double.parseDouble(nextLine[i]);
    }

OR

    double[] polyval = new double[3];
    polyval = Arrays.stream(usrIn.nextLine().split(" ")).mapToDouble(Double::parseDouble).toArray();

Neither of these solutions takes into account bad input, so you will want to add some checks to make sure the input is in format that is acceptable

for instance:

before the for loop, you could add a check

if (nextLine.length != 3) {
    System.out.println("input should have length of 3");
    System.exit(1);
} 
nLee
  • 1,320
  • 2
  • 10
  • 21
  • 1
    If OP doesn't even understand that a `String` doesn't magically assign to a `double[]`, then Streams is definitely way beyond OP's current level of understanding. I think the answer would be more helpful (useful) to OP if you showed non-Stream code. – Andreas May 23 '19 at 16:16