-2

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();

    }

}
NeXz
  • 1
  • 3
  • Show us the code *here* – TheLostMind Nov 21 '15 at 14:32
  • Check [this](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) . – TheLostMind Nov 21 '15 at 14:35
  • How should that page provide any "user input" (using `System.in`)? It doesn't even try to emulate that like https://ideone.com/. – Tom Nov 21 '15 at 14:35
  • 2
    What do you want your application to do when run on that site? It doesn't seem like the site offers any way to provide user input. – sepp2k Nov 21 '15 at 14:35
  • @VinodMadyalkar NeXz isn't using `nextInt` and the problem isn't that input is being skipped, it's what happens if there is no input available. – sepp2k Nov 21 '15 at 14:36
  • @sepp2k You can get the same problem if one uses `next()` and then `nextLine()`, but you're right, this is not the probleme here. – Tom Nov 21 '15 at 14:37
  • @NeXz Please specify the problem. – Yassin Hajaj Nov 21 '15 at 14:45
  • The problem is with the online compiler he is using to test his code. The online compiler cannot (or does not properly) emulate the input (System.in) from keyboard which is needed for Scanner object. Tested on a different online compiler and it works without any issues. He should not rely on online compilers. – Raf Nov 21 '15 at 15:11
  • @sepp2k - I saw the OP using `next()` , so I thought it would be better to let him know about issues related to using `nextXX` methods after `nextLine()` :) – TheLostMind Nov 21 '15 at 17:17
  • 1
    Possible duplicate of [Scanner next() throwing NoSuchElementException for some online compilers](https://stackoverflow.com/questions/39766488/scanner-next-throwing-nosuchelementexception-for-some-online-compilers) – Bernhard Barker Jun 10 '17 at 16:10

1 Answers1

1

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.

Community
  • 1
  • 1
Raf
  • 6,620
  • 1
  • 35
  • 54
  • Thank you all for the fast answers :) you helped me a lot. – NeXz Nov 21 '15 at 15:15
  • You are welcome, if this answer helped answer the question then please mark it as accepted. – Raf Nov 21 '15 at 15:21
  • 1
    The code does compile correctly, it just fails at runtime because stdin is closed. I wouldn't call that a failure on the web IDE's part, though (a missing feature certainly in that there is no way to provide input, but given that there is no input, the behavior is 100% correct). The code would behave the same way locally if you closed the stream without entering input or made it read from an empty file. – sepp2k Nov 21 '15 at 15:24
  • @sepp2k agree with the lack of stdin feature in the web IDE. – Raf Nov 21 '15 at 17:04