Bonjour à tous.

J'ai pour projet de créer une bataille navale en java. J'ai créé une classe Action qui a pour but de demander au joueur ce qu'il souhaite faire : "fire" pour tirer ou "view" pour afficher la grille de jeux. Voici ma classe Action :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package fr.enseirb.battleship;
 
import java.util.Scanner;
 
public class Action {
 
    private int x;
    private int y;
    private String act;
    public Action(){
        this.act="action";
        this.x=0;
        this.y=0;
    }
 
    public void get(){
         Scanner sc = new Scanner(System.in);
            System.out.println("Select an action (fire or view) :");
            this.act = sc.next();
            System.out.println("You choose : " + this.act);
            if(this.act.equals("fire")){
                System.out.println("Select the target position X Y");
                this.x = sc.nextInt();
                 this.y = sc.nextInt();
                System.out.println("You choose : " + this.x +","+this.y);
            }
 
    }
 
    public String getact(){
        return this.act;
    }
    public int getx(){
        return this.x;
    }
    public int gety(){
        return this.y;
    }
 
 
}
Lorsque je la teste dans une classe Test elle fonctionne sans problème :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package fr.enseirb.battleship;
 
import fr.enseirb.battleship.ship.*;
 
import java.util.Scanner;
 
import fr.enseirb.battleship.grid.*;
public class Test {
 
    public static void main(String[] args){
 
 
 
    Action action = new Action();
 
 
 
            action.get();
            System.out.println(action.getact());
 
            if(action.getact().equals("fire")){
 
 
                System.out.println("on est dans fire");
            }
            else if(action.getact().equals("view")){
 
                System.out.println("on est dans view");
            }
 
            else if(action.getact().equals("debug")){
 
                System.out.println("on est dans debug");
            }
 
 
        }
 
 
 
    }
Mais quand je l'utile dans ma classe Game cela ne fonctionne plus :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package fr.enseirb.battleship;
 
import fr.enseirb.battleship.ship.*;
import fr.enseirb.battleship.shot.*;
import fr.enseirb.battleship.draw.*;
 
import java.util.Scanner;
 
import fr.enseirb.battleship.grid.*;
 
public class Game {
 
    private int run;
    private int x;
    private int y;
    private View view = new View();
    private Debug debug = new Debug();
 
public Game(){
 
}
 
    public void rungame(ShipList playershiplist, ShipList aishiplist, Grid grid){
 
 
        PlayerShot playershot = new PlayerShot(playershiplist, grid);
        AiShot aishot=new AiShot(aishiplist, grid);
        Action action=new Action();
        action.get();
 
 
        if(action.getact().equals("fire")){
 
 
            playershot.playershot(action.getx(), action.gety());
            aishot.randomshot();
        }
        else if(action.getact().equals("view")){
 
            view.view(grid, aishiplist, playershiplist, aishot.getshotlist(), playershot.getshotlist());
        }
 
        else if(action.getact().equals("debug")){
 
            debug.debug(grid, aishiplist, playershiplist, aishot.getshotlist(), playershot.getshotlist());
        }
 
 
    }
 
}
J'obtient le message d'erreur suivant :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1371)
    at fr.enseirb.battleship.Action.get(Action.java:19)
    at fr.enseirb.battleship.Game.rungame(Game.java:28)
    at fr.enseirb.battleship.App.main(App.java:36)
Et je n'arrive pas à comprendre pourquoi ..