.split() Method is NOT Allowed
I was got some help earlier from a helpful guy! I was just wondering if anyone could help me modify this a little bit more,
The code is meant for an assignment and It is based on the input from the scanner. It has two other classes but this is the one of interest.
The code is working at the moment, however The things that have to be input are things like **U5, D10**
etc. That works fine. However I need the code to be able to read multiple String off one one line whilst seperating them like they are now. Like say for example **D10 U5 L4**
, all from one line for just one player out of two. The code at the moment is not recognizing it as one line and instead if assigning the second typed thing to the second player.
Any tips?
Thanks
import java.util.Scanner;
class Asgn2
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
Player me = new Player("Player1");
Player opponent = new Player("player2");
int startingLoop = 0;
String strA;
int turn =1;
System.out.print("How many turns will the game have: ");
int turnsInGame = scan.nextInt();
System.out.print("How many moves does each player have each turn: ");
int numberOfTurns = scan.nextInt();
for(int i = turnsInGame; startingLoop < i; startingLoop++)
{
System.out.print("Turn " + turn++ + "\n");
System.out.print("Player 1 what are your " + numberOfTurns + " move(s): ");
String userInput = scan.next();
System.out.print("Player 2 what are your " + numberOfTurns + " move(s): ");
String userInputOne = scan.next();
for (int j = 0; j < userInput.length() - 1; j++)
{
char letter = userInput.charAt(j);
String num = "";
for(int k= j + 1; k < userInput.length(); k++)
{
j++;
if(userInput.charAt(k)!=' ')
{
num+=userInput.charAt(k);
}
else
{
break;
}
}
int integer = Integer.parseInt(num + "");
strA = Character.toString(letter);
switch(strA) //For player oneChooses which value to add or subtract from based on what is input.
{
case "U":
me.move(moveSteps.UP , integer);
break;
case "D":
me.move(moveSteps.DOWN, integer);
break;
case "L":
me.move(moveSteps.LEFT, integer);
break;
case "R":
me.move(moveSteps.RIGHT, integer);
break;
}
//Player 2
for (int playerTwo = 0; playerTwo < userInputOne.length() - 1; playerTwo++)
{
char letterTwo = userInputOne.charAt(0);
String numTwo = "";
String strB = Character.toString(letterTwo);
for(int m= playerTwo + 1; m<userInput.length(); m++)
{
playerTwo++;
if(userInputOne.charAt(playerTwo)!=' ')
{
numTwo+=userInputOne.charAt(playerTwo);
}
else
{
break;
}
}
int stepsMoved = Integer.parseInt(numTwo + "");
switch(strB) //For player two
{
case "U":
opponent.move(moveSteps.UP , stepsMoved);
break;
case "D":
opponent.move(moveSteps.DOWN, stepsMoved);
break;
case "L":
opponent.move(moveSteps.LEFT, stepsMoved);
break;
case "R":
opponent.move(moveSteps.RIGHT, stepsMoved);
break;
}
}
}
System.out.print(me);
System.out.print(opponent);
}
}
}