11 years ago*

Comment has been collapsed.

can I ask you a question? why do you use scanner.close() at the end?
edit: this question has nothing to do with the error but in never used .close() on Scanner and I don't know what is used for in this case.

11 years ago
Permalink

Comment has been collapsed.

so it garbage collect scanner(object) and stops scanning .

11 years ago
Permalink

Comment has been collapsed.

thanks. Another question if I don't use it, there is so much difference in functionality, performance or I don't know something else?

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

you will understand and feel th difference if you're making database, if you didnt close the sql, sqlserver.exe will exist in the process and consume 25% of all ur cpu usage (if u have quad core)

11 years ago
Permalink

Comment has been collapsed.

I already had problems with this in c using processes generated by the program, cause stupid errors i make sometimes... but i thought that java would have got rid by things that are no more usefull.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Did you ever
import java.util.scanner?

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

I got basic java class like 3 years ago and forgot almost all of it .

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Could you host somewhere archive with whole project, it could help us a bit?

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Better paste it to pastebin.com or sth like that. Cuz some things are messed up (replaced by eg. <).

11 years ago
Permalink

Comment has been collapsed.

"Your problem is caused by trying to read too much input. Imagine that you have 3 items in your file. Then you try and read 4 items from it. Obivously the fourth time you attempt to read there is nothing left, hence the exception. Now it is your job to find out where and or why you are doing this."

google

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Yep, it will be most probably the right answer...

Btw, what about debugging? :) Trust me, it'll help you a lot :)

11 years ago
Permalink

Comment has been collapsed.

I'm don't think it's that. I tried it on my computer and it works fine. It has probably something to do with his input not working like it should.

11 years ago
Permalink

Comment has been collapsed.

try printing the value of scanner.nextint enclosed by a catch try block

11 years ago
Permalink

Comment has been collapsed.

I can do Helloworld with 5 different languages but I have no idea what your problem is.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Try this:
System.out.println("Where do you want to bomb? (Input row first, then column)");
String str = scanner.nextLine();
try {
y = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("That wasn't a number");
continue;
}
y--;

11 years ago
Permalink

Comment has been collapsed.

That should not be the problem and would actually reduce the program functionality as it would force the user to input each number in its own line instead of writing them all in the same line separated by spaces.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Did you do the same thing for x?

Here's another solution that will allow you to read 2 nrs from the same line

System.out.println("Where do you want to bomb? (Input row first, then column)");
while (true) {
try {
y = scanner.nextInt();
break;
} catch (NoSuchElementException e) {
scanner.next();
}
}
y--;

Again, do the same for x.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Same exception?

11 years ago
Permalink

Comment has been collapsed.

The "NoSuchElementException" is caused by trying to read after the end of the input. If you were reading the input from a file it would be easy to see, but from the standard input it may be difficult.
Be careful not to press Control+D or Control+Z in the console where you are writing the input as it is sometimes (depending on the platform) as the end of file.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Am i the only one not getting how you give the player time to enter something?

11 years ago
Permalink

Comment has been collapsed.

Since the program reads from the standard input (System.in), the call to scanner.nextInt() waits for the user to write a number followed by a space or new line before continuing with the next statement.

11 years ago
Permalink

Comment has been collapsed.

thanks never used the class scanner so i did not know if it waits for input

11 years ago
Permalink

Comment has been collapsed.

Are you sure you have your System.in in the right place? I'm not sure how scanners work exactly, but it looks like you are only giving it input once on initialisation and not in the loop, after it asks for the input, so when it looks for the next int there isn't any input there yet.

11 years ago
Permalink

Comment has been collapsed.

System.in is the standard input in Java, is the console where the user is actually writing the numbers, there is no need to reinitialize the Scanner object.

11 years ago
Permalink

Comment has been collapsed.

It gives it an input stream, namely the program STDIN, which is probably keyboard input (I doubt he changed it).

11 years ago
Permalink

Comment has been collapsed.

Interesting... I know very little about java code, I am learning VB and Python and will soon start C++.
All I can say is that I know your pain very well.

11 years ago
Permalink

Comment has been collapsed.

Looking at the nextInt from the Scanner class, you can see that it said

NoSuchElementException - if input is exhausted

and you also said that hasNext just skip the damn line, you know why? because I'm assuming there is nothing next, so trying to call nextInt will clearly throw the input is exhausted exception.

the issue is here

y = scanner.nextInt();
y--;
if (y > 4 || y < 0)

then if your y is < 0 (or higher than 4, but I'm assuming it's not) , it call another nextInt() without checking for the hasNext.

     x = scanner.nextInt();
                    x--;
                    if (x > 4 || x < 0)

so let's say in the scanner there is only 1 item, nextInt from your y will return the first item, but then when it hit the x, it will try to find the second item (which there is none, and will throw that exception)

I hope it helped a little.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

That part of the code works fine on my computer. Does the Yes and No part work like it should?

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Hmm...ok, happened to me too with a couple of times, but with some Android projects. If it's not too much of a hassle, try to make a new project and copy your classes over and see if it fixes itself.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

That would be the exact explanation if the program were reading the input from a file (wich can end), but since it is reading from the standard input, the call to nextInt() would wait for the user to finish writing.

Another option (I'm guessing here) is that the input is being redirected to STDIN from a file, something like:
java program < input.txt

11 years ago
Permalink

Comment has been collapsed.

Yeah, seems more like that's the problem, unless the part of scanner.nextLine() works fine.

11 years ago
Permalink

Comment has been collapsed.

l don't have access to a computer to check now, but a quick Google search suggested that the problem is with your closing of the scanner in the placement class. Apparently that also closes System.in, so when you make a new Scanner on System.in, it won't find anything.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

i tried your code and this is the problem
the second time that you call the method it reports that System.in is closed
this is the source code that throws the exception
private void throwFor() {
skipped = false;
if ((sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException();
else
throw new InputMismatchException();
}

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Close it when you exit the program.

11 years ago
Permalink

Comment has been collapsed.

Ahh I see. I actually never got out of the loop.

11 years ago
Permalink

Comment has been collapsed.

Try this.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

This is mainly a guess, since I'm in a computer without Java and I'm programming from notepad, but I took a look at the whole code and I think you could correct it by using a sing program wide Scanner object this way:
1) put "Scanner scanner = new Scanner(System.in);" in the main method, right after the field declaration.
2) remove "Scanner scanner = new Scanner(System.in);" from every other part of your program.
3) change firstPlacementPlayer method to firstPlacementPlayer(short[][] field, Scanner scanner)
4) change bombingPlayer method to bombingPlayer (short[][] field, Scanner scanner)
5) change the calls to firstPlacementPlayer and bombingPlayer to firstPlacementPlayer(field, scanner) and bombingPlayer(field, scanner)
6) remove every instance of scanner.close();

The idea is to have only one Scanner instance to avoid locking the standard input.

11 years ago
Permalink

Comment has been collapsed.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

:D

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

That is what I would do also

11 years ago
Permalink

Comment has been collapsed.

Personally I'm not trusting this scanner class, as that seems to be causing the problem somehow.

I would go for a BufferedReader for the input and then string.split to separate the number strings and Integer.parseInt to get the numbers as ints.

11 years ago
Permalink

Comment has been collapsed.

The Scanner class is standard in Java, part of the java.util package. Its main purpose is to avoid having to parse the input by hand.

11 years ago
Permalink

Comment has been collapsed.

Did you look at the API?
More specifically this
My guess would be that it's because you try reading constantly; you never wait userinput. As such, it breaks.
I'd give you a pastebin of something that works, but it'll take me a bit to find. Hang in there. :P

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

You're quite right. It's been too long since I've worked with scanners. :P

11 years ago
Permalink

Comment has been collapsed.

The nextInt() method waits for user input because it is reading from System.in

11 years ago
Permalink

Comment has been collapsed.

You're also right! :P

11 years ago
Permalink

Comment has been collapsed.

This is not for the problem but I would make the below code (and others like it) a for loop not a while loop
while (placement != 5)
{
y = (short) r.nextInt(5);
x = (short) r.nextInt(5);
field[y][x] = 1;
placement++;
}

for (int i = 0; i < 5; i++)
{
y = (short) r.nextInt(5);
x = (short) r.nextInt(5);
field[y][x] = 1;
}

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 3 years ago.

11 years ago
Permalink

Comment has been collapsed.

Don't close the scanner... by doing so early, you end up closing the System.in...

EDIT
Oh as I noticed that someone had already mentioned :p

EDIT
You should do what someone mentioned earlier and simply open up the inputstream in the beginning of your program and then simply close it after the game is over and the program is about to exit.

11 years ago
Permalink

Comment has been collapsed.

Closed 11 years ago by TheNamlessGuy.