0

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






 }
}
 }
Jon Roy
  • 53
  • 1
  • 11
  • start by reading the API doc for the Scanner class's next() and the nextLine() methods. – NormR Nov 08 '15 at 01:08
  • @NormR Nvm got it, I forgot the scan.nextLine(); after my original call, Now it only reads the first value Like if i typed L45 and U45, it disregards the U45. Any tips? – Jon Roy Nov 08 '15 at 01:28

3 Answers3

0

Once you assign the input to a String, use the .split() method to split the string into an array. To use the .split(), put in the character you want to split it with. In this case a space. For example use this for your current project: .split(" "). Once you split it, you can access it just like any array.

Update:
first use the .nextLine() and assign it to a temporary string variable. Then you can create another scanner and put a string in. For example:

Scanner sc = new Scanner(YOUR TEMPORARY VARIABLE);

you can now use the .next() to get individual strings.

Suhas Bacchu
  • 37
  • 1
  • 1
  • 6
0

Here is the Asgn2 class

import java.util.Scanner;

public class Asgn2 {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("What is your name player 1: ");
    String p1name = scan.nextLine();
    System.out.print("What is your name player 2: ");
    String p2name = scan.nextLine();
    Player p1 = new Player(p1name);
    Player p2 = new Player(p2name);

    System.out.print("How many turns will the game have: ");
    int numTurns = scan.nextInt();
    System.out.print("How many moves does each player have each turn: ");
    int numMoves = scan.nextInt();

    for (int turn = 1; turn <= numTurns; turn++) {
        System.out.println("----------------");
        System.out.println("Turn number " + turn);
        System.out.println("----------------");

        for (int player = 1; player <= 2; player++) {
            System.out.print("Player " + player + " what are your " + numMoves + " move(s): ");
            for(int move=1;move<=numMoves;move++){
                String currMove = scan.next();//splits at space;
                char dir = currMove.charAt(0);//gets dir
                String temp="";
                for(int index=1;index<currMove.length();index++){
                    temp+=currMove.charAt(index);
                }
                int dist = Integer.parseInt(temp);
                if(player==1){
                    p1.move(dir, dist);
                }else if(player==2){
                    p2.move(dir, dist);
                }
            }

            System.out.println("Player 1 is at " + p1.getPos() + " and Player 2 is at " + p2.getPos());
            System.out.println();
        }
    }
}
}

And here is the Player class

public class Player {
    private String name;

    private int locX = 0;
    private int locY = 0;

    public Player(String name) {
        this.name = name;
    }

    public void move(char dir, int numSteps) {
        switch (dir) {
        case 'U':
            locY += numSteps;
            break;
        case 'D':
            locY -= numSteps;
            break;
        case 'L':
            locX -= numSteps;
            break;
        case 'R':
            locX += numSteps;
            break;
        }
    }

    public String getPos() {
        return "(" + locX + ", " + locY + ")";
    }

    public String getName() {
        return name;
    }
}

And before anyone goes and says that posting blocks of code does not help OP, I am in a chatroom with him in which I explain this stuff, so dont hate :)

Kore
  • 436
  • 2
  • 14
-1

call the method .nextLine() instead of .next(). I think that should solve your problem.

Suhas Bacchu
  • 37
  • 1
  • 1
  • 6
  • Calling nextLine wouldn't fix it it though, there is multiple strings on one line, I cant seem to access the second String. When i called .nextLine(). it broke the code. – Jon Roy Nov 08 '15 at 01:52