Help: Java error message re: incompatible types
I'm writing a program for my intro to Java class. I'm getting an error message, and I can't figure out what exactly it's telling me or how to resolve the issue. This is the message:
packageCost.java:17: incompatible types
found : void
required: java.lang.String
input = System.out.print("Please enter the weight of " +
^
packageCost.java:22: incompatible types
found : void
required: java.lang.String
input = System.out.print("How many miles is this " 开发者_运维百科+
^
2 errors
Any help would be appreciated.
System.out.print does not return anything and you are trying to collect its return value in a variable.
Your attempting to assign a String to "System.out.print("Please enter..");
System.out.print returns "void" which is not String, thus incompatible types.
It looks like your trying to do console input. You could use a Scanner to do this.
Try something like
Scanner scanner = new Scanner(System.in);
System.out.println("Enter input: ");
String input = scanner.nextLine();
Read about Scanner class, Just google it.
better:
String input = System.console().readLine("Please enter the ..");
The Scanner won't work without declaring at the top of the document:
import java.util.Scanner;
精彩评论