Java: scanner compile errors
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 (eg 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();
}
}
If the code runs in IDE then everything is correct. The way Scanner work is:
Scanner input = new Scanner(System.in);
System is java.lang.System
and it has static objects namely in being of type InputStream
, out and err being of type PrintStream
.
Basically System.in
is the InputStream object that is connected to a source from where it should expect the data - they Keyboard.
The online IDE are supposed to be designed in such a way that it should emulate the InputStream and Input via Keyboard etc and the Compile Java that you mentioned where it is not running your code correctly is having issue emulating the input from the Keyboard the way Scanner expect it. The fault is not from your code but, from the online compiler. To proof my point please try the different Java Compiler you find in this link.
If you want to understand how Scanner works in java then, see the answer I have given to this question in Stackoverflow. There I have explained in detailed what is Scanner, how it works.
链接地址: http://www.djcxy.com/p/17218.html上一篇: 扫描仪内的扫描仪错误
下一篇: Java:扫描器编译错误