input the array of integers
1 3 5
4^D
s 1 3 5
input the target integer
target -1
Process finished with exit code 0
Ron McLeod wrote:You could just trying to read integers from the scanner - if the next token can not be transformed to an int, then Scanner#hasNextInt will return false.
input the array of integers
1 3 5 .
al [1, 3, 5]
input the target integer
target -1
Process finished with exit code 0
No, don't use ctrl‑D; it can close System.in, leaving you with no way to reopen it. And it's probably better to call it System.in rather than stdin when writing Java® please.Anil Philip wrote:. . . I found that using CTRL-D to end the array input will reset stdin. . . .
Don't confuse arrays and array lists. A list isn't an array, and you are creating a list.Anil Philip wrote:. . . I want to read in an array of integers . . .
Campbell Ritchie wrote:
I think I would use a Stream (some of these methods were not available in Java8).
Campbell Ritchie wrote:passing it to a new Scanner object.
input the array of integers
1 3 5 . 4
Exception in thread "main" java.util.ConcurrentModificationException
at java.base/java.util.Scanner$TokenSpliterator.tryAdvance(Scanner.java:2851)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:575)
at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.base/java.util.stream.IntPipeline.toArray(IntPipeline.java:562)
at two_sum.TwoSum_Scanner.main(TwoSum_Scanner.java:14)
Process finished with exit code 1
Ron McLeod wrote:There is probably a better way to work with a stream of tokens, but this is my first thought:
java: incompatible types: invalid method reference
method intValue in class java.lang.Integer cannot be applied to given types
required: no arguments
found: java.lang.String
reason: actual and formal argument lists differ in length
Exception in thread "main" java.lang.NumberFormatException: For input string: "."
No. You are looking at the token previously read, not the next token. I have already told you, the overhead of creating multiple objects is tiny compared to the time it takes to enter numbers.Anil Philip wrote:. . . Is there a way to reuse the Scanner instead of creating a new one each time?
That is because you are not using what I wrote. You are trying to do two things simultaneously with the Scanner, which it doesn't like. I have only seen that exception with Iterators . . . . but a Scanner IS‑AN Iterator<String>.When I try your version, I get a ConcurrentModificationException . . .
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|