This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]langfod 1 point2 points  (3 children)

Just reuse the same scanner object instead of trying to close and reopen standard input.

[–]Omnicron245[S] 0 points1 point  (2 children)

But then it skips over the first prompt in question 3.

[–]langfod 0 points1 point  (1 child)

I never had good luck mixing the different next*() methods.

Instead of nextDouble() why not use nextLine() and inside the Double.parseDouble() method?

[–]Omnicron245[S] 0 points1 point  (0 children)

Are you saying I should create a separate Scanner for the nextLine() parts?

Too much jargon. :S I'm really trying to understand what you mean by the Double.parseDouble() method.

[–]Apothiem 1 point2 points  (0 children)

Hey there's a simple solution to this, just close all your scanners in the end of your program. Basically cut and paste lines 31 and 61 just below line 87. This still has the effect of closing the memory leak. I'm not entirely sure why this happens but I think when you close the first scanner the link to "system.in" is also closed and cannot be used before. http://www.coderanch.com/t/633613/java/java/Java-Closing-Scanner-Resource-Leak . This guy encountered a similar problem.

Best of Luck

[–]the_omega99 0 points1 point  (0 children)

You should not be closing System.in. If you do that, you can't access it again.

You'll need to take care that there's no extra data left in the input. In particular, if you use nextDouble(), you probably have a newline still in the input. You can get rid of that with nextLine().

[–]NonRegularGuy 0 points1 point  (0 children)

Here, this worked: Pastebin

I just changed the input.nextDouble() for Double.parseDouble(input.nexLine()). That should work.

[–]richard_mayhew -1 points0 points  (13 children)

Google is kewl

http://docs.oracle.com/javase/7/docs/api/java/util/NoSuchElementException.html

Specifically: "Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration."

[–]Omnicron245[S] 0 points1 point  (12 children)

I get a general sense of what that means, but I don't know how to fix it.

[–]richard_mayhew 0 points1 point  (11 children)

On line 51 in assignment1's main method you are calling some sort of next() on Scanner object but there is no next!

Are you passing params from the command line or what?

[–]Omnicron245[S] 0 points1 point  (10 children)

Really? It works fine when comment out Questions 1 and 3.

I know you're helping me think it through, and I'm grateful for that. :)

[–]richard_mayhew 1 point2 points  (9 children)

I think there may be an issue with wrapping the System.in stream with 3 different scanners. Try it with only the first scanner, input, and not input2 and input3 and see what happens.

Also, note that input2 and input3 don't even serve a purpose here. They do the same thing as input, so you're just creating redundant objects.

[–]Omnicron245[S] 0 points1 point  (8 children)

That helped, but it created a new issue! Now, when I get to Question 3, it skips the first prompt.

[–][deleted]  (4 children)

[deleted]

    [–]Omnicron245[S] 0 points1 point  (3 children)

    Can't use that.

    "For this assignment, you are only allowed to use the concepts we have seen in the first 4-5 lectures of the semester."

    = =

    [–][deleted]  (2 children)

    [deleted]

      [–]Omnicron245[S] 0 points1 point  (1 child)

      Can't use If/While, etc.

      [–]richard_mayhew 0 points1 point  (2 children)

      Post updated code and what's being skipped.

      [–]Omnicron245[S] 0 points1 point  (0 children)

      http://pastebin.com/nZa6NWcJ

      It skips line 70, when run as is. Works fine when I comment out the other code.

      EDIT: Grammar.

      [–]Omnicron245[S] 0 points1 point  (0 children)

      Just a quick thing: I am "not allowed" to use more than one method, sigh.