I got basic java class like 3 years ago and forgot almost all of it .
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."
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--;
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.
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.
Comment has been collapsed.
Am i the only one not getting how you give the player time to enter something?
Comment has been collapsed.
thanks never used the class scanner so i did not know if it waits for input
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.
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.
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.
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
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.
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();
}
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.
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.
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
Comment has been collapsed.
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;
}
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.
Comment has been collapsed.
286 Comments - Last post 33 minutes ago by hbouma
173 Comments - Last post 51 minutes ago by hbouma
642 Comments - Last post 1 hour ago by Oppenh4imer
58 Comments - Last post 1 hour ago by RobbyRatpoison
864 Comments - Last post 2 hours ago by Ashtart
255 Comments - Last post 3 hours ago by XfinityX
30 Comments - Last post 11 hours ago by TinTG
14 Comments - Last post 24 seconds ago by GeekDoesStuff
20 Comments - Last post 52 seconds ago by Studentpg
79 Comments - Last post 44 minutes ago by axolotlprime
6,402 Comments - Last post 1 hour ago by Oppenh4imer
527 Comments - Last post 1 hour ago by okamiking
8,209 Comments - Last post 1 hour ago by ClapperMonkey
96 Comments - Last post 1 hour ago by Vampus
Comment has been collapsed.