i'm currently working on a simple battleship-game. i'm using the Java-scanner to get coordinates from the user. if I run this code in eclipse i get no errors, everything is just fine. but if I'm running the code in another compiler (e.g. http://www.compilejava.net/) i get this error:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at SchiffeVersenken.attack(SchiffeVersenken.java:57) at SchiffeVersenken.main(SchiffeVersenken.java:38)
so i know the problem is has to do with the scanner and the ".nextLine()"-method, but im new to java and don't how solve this.
import java.util.Scanner;
public class SchiffeVersenken {
// "Lebenspunkte" der Spieler
static int userLife = 30;
static int enemyLife = 30;
// Spielbrett beider Spieler
static char[][] user = {
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '#', '#', '#', '#', '#', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '#', '#', '#', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' },
{ '.', '.', '.', '.', '.', '#', '#', '#', '#', '#' }
};
static char[][] enemy = {
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
{ '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(userLife > 0 && enemyLife > 0) {
attack(s);
defend(s);
}
s.close();
if(userLife == 0) {
System.out.println("Alle Schiffe sind versenkt worden. Sie haben leider verloren!");
} else {
System.out.println("Alle Schiffe des Gegners sind versenkt worden. Sie haben gewonnen!");
}
}
public static void attack(Scanner s) {
String inputHit;
int inputRow, inputColumn;
System.out.println("Bitte Zeile des Ziels eingeben: ");
inputRow = Integer.parseInt(s.nextLine());
System.out.println("Bitte Spalte des Ziels eingeben: ");
inputColumn = Integer.parseInt(s.nextLine());
System.out.println("Koordinaten des Zeils: " + inputRow + "/" + inputColumn);
System.out.println("Ziel getroffen? (y oder n)");
inputHit = s.nextLine();
if(inputHit.equals("y") | inputHit.equals("n")) {
if(inputHit.equals("y")) {
enemy[inputRow][inputColumn] = 'X';
System.out.println("Boom! Guter Schuss!");
enemyLife -= 1;
} else {
enemy[inputRow][inputColumn] = 'O';
System.out.println("Oh je! Leider nicht getroffen!");
}
} else {
System.out.println("Ziel getroffen? (y oder n)");
inputHit = s.next();
}
print(enemy);
}
public static void defend(Scanner s) {
int inputRow, inputColumn;
System.out.println("Bitte Zeile des Ziels eingeben: ");
inputRow = Integer.parseInt(s.nextLine());
System.out.println("Bitte Spalte des Ziels eingeben: ");
inputColumn = Integer.parseInt(s.nextLine());
System.out.println("Koordinaten des Zeils: " + inputRow + "/" + inputColumn);
if(user[inputRow][inputColumn] == '#') {
user[inputRow][inputColumn] = 'X';
System.out.println("Boom! Schiff unter Beschuss!");
userLife -= 1;
} else {
user[inputRow][inputColumn] = 'O';
System.out.println("Gott sei Dank! Nicht getroffen!");
}
print(user);
}
public static void print(char[][] grid) {
System.out.println(" 0123456789");
for(int i=0; i<10; i+=1) {
System.out.print(i);
for (int j=0; j<10; j+=1) {
System.out.print(grid[i][j]);
if(j == 9) {
System.out.println();
}
}
}
System.out.println();
}
}