Home > Blockchain >  (SOLVED) Scanner refuses to take the next (or any) input (SOLVED)
(SOLVED) Scanner refuses to take the next (or any) input (SOLVED)

Time:05-08

For educational/debugging purposes only

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

Source of bug:

while(input.hasNextLine())
{
    lines.add(input.nextLine); // WHERE THE ERROR OCCURS
}

^This^ should be like so:

while(input.hasNextLine())
{
    lines.add(input.nextLine()); // including ()
}

Here are links to more information:

whole Scanner class: https://www.tutorialspoint.com/java/util/java_util_scanner.htm

.next() specifically: https://www.tutorialspoint.com/java/util/scanner_next.htm

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.

  • Related