hey everyone, thanks for taking a look at my code. So i am building a text based battleship program for school.
Ok so this is the problem. I ask the user to select a number(1 - 5), each is a different ship. I set the shipNum var equal to that number. Then i pass it my placeShip() function, which will select the correct ship and attempt to place it on the battleship board. After it chooses the correct ship, it is supposed to ask the user for coordinates to place the ship, and then place it on the board. The error i am getting supposedly means there is no input there for it to get, but i dont really understand why.
What has worked: If i dont use shipNum and i just use a particular function like placeBattleship() or placePatrolBoat() it works fine. It asks the user for coordinates and then places it correctly. Also, if i just ask the user for a number and dont try to place a ship it works fine.
Any suggestions or comments are appreciated. I am pretty new to java so take it easy on me lol.
Ok so this is the problem. I ask the user to select a number(1 - 5), each is a different ship. I set the shipNum var equal to that number. Then i pass it my placeShip() function, which will select the correct ship and attempt to place it on the battleship board. After it chooses the correct ship, it is supposed to ask the user for coordinates to place the ship, and then place it on the board. The error i am getting supposedly means there is no input there for it to get, but i dont really understand why.
What has worked: If i dont use shipNum and i just use a particular function like placeBattleship() or placePatrolBoat() it works fine. It asks the user for coordinates and then places it correctly. Also, if i just ask the user for a number and dont try to place a ship it works fine.
Any suggestions or comments are appreciated. I am pretty new to java so take it easy on me lol.
boolean isUsed; int count = 0; int shipNum; printShipList(); //while(count < 5){ shipNum = selectShip(); //System.out.println(shipNum); person.placeShip(shipNum); //person.placeShip(shipNum); //computer.placeRandomShips(); //computer.printGrid(); //count++; //} person.printGrid();
public void placeShip(Ship ship){ Scanner s = new Scanner(System.in); boolean notPlaced = true; char row = 'a'; int x = 0; int y = 0; int length = ship.getShipLength(); int num = 0; int orien; String input; String corArray[] = new String[2]; while(notPlaced){ System.out.println("Enter coordinate to place ship "); input = s.next(); for(int i = 0; i < 2; i++){ corArray[i] = input.substring(i, i + 1); } y = Integer.parseInt(corArray[1]);
public void placeShip(int shipNum){ switch(shipNum){ case 1: board.placeShip(patrolBoat); break; case 2: board.placeShip(destroyer); break; case 3: board.placeShip(submarine); break; case 4: board.placeShip(battleship); break; case 5: board.placeShip(aircraftCarrier); break; } }
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at Grid.placeShip(Grid.java:49) at Player.placeShip(Player.java:54) at Game.main(Game.java:17)