"didn't you forget the brackets? .nextLine() .next()" – experiment unit 1998X
"nextLine() is a function when calling function in java we need parenthesis after a function name to invoke a function." – bhawesh agrawal
Current error message "cannot find symbol: variable nextLine in variable input of type Scanner"
Eventually it is going to do much more than this but I don't even understand why it won't read...so it may take a bit XD
Method so far:
public static void flipCoins(Scanner input)
{
ArrayList<String> lines = new ArrayList<>();
while(input.hasNextLine())
{
lines.add(input.nextLine);
// WHERE THE ERROR OCCURS
}
System.out.print(lines.toString());
}
These are the links I have been trying to debug with:
whole Scanner class: https://www.tutorialspoint.com/java/util/java_util_scanner.htm
.next() specifically: https://www.tutorialspoint.com/java/util/scanner_next.htm
EDIT FOR MORE
I also receive this error: "cannot find symbol: variable next in variable input of type Scanner", with the following:
lines.add(input.next);
CodePudding user response:
you are missing parenthesis"()", nextLine() is a function when calling function in java we need parenthesis after a function name to invoke a function. similar to what you did here
while(input.hasNextLine())
this will work
public static void flipCoins(Scanner input)
{
ArrayList<String> lines = new ArrayList<>();
while(input.hasNextLine())
{
lines.add(input.nextLine());
}
System.out.print(lines.toString());
}
CodePudding user response:
You've made this far more complicated than it actually is.
In java, method calls must end in parentheses; they aren't optional. Methods and Fields have separate namespaces - input.next
, without the parens, is a field reference (so 'but why won't the language add it!' is a non-starter, it wouldn't be backwards compatible).
Just replace input.next
with input.next()
, and input.nextLine
with input.nextLine()
.
CodePudding user response:
The call
lines.add(input.nextLine);
Needs ( ) on the end of the .nextLine() because it is a void method within the Scanner class. it could even (but usually shouldn't) be rewritten:
String input = new Scanner(file).nextLine();
Because it is essentially just a piece of the Scanner object.