如何通过扫描仪读取同一行的多个字符串没有Split();

时间:2022-10-25 00:12:14

.split() Method is NOT Allowed

.split()方法不允许

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.

代码正在工作,但是必须输入的东西是像** U5,D10 **等。这样可以正常工作。但是我需要代码能够在一行中读取多个String,同时将它们分开,就像它们现在一样。比如说例如** D10 U5 L4 **,所有这一切都只来自一个玩家中的两个。目前的代码并没有将其识别为一行,而是将第二种类型的东西分配给第二个玩家。

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






 }
}
 }

3 个解决方案

#1


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.

将输入分配给String后,使用.split()方法将字符串拆分为数组。要使用.split(),请输入要将其拆分的字符。在这种情况下一个空间。例如,将此用于当前项目:.split(“”)。拆分后,您可以像任何阵列一样访问它。

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:

更新:首先使用.nextLine()并将其分配给临时字符串变量。然后你可以创建另一个扫描仪并输入一个字符串。例如:

Scanner sc = new Scanner(YOUR TEMPORARY VARIABLE);

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

您现在可以使用.next()来获取单个字符串。

#2


0  

Here is the Asgn2 class

这是Asgn2类

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

这是Player类

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 :)

在任何人去之前说发布代码块对OP没有帮助,我和他在聊天室中解释这些东西,所以不要讨厌:)

#3


-1  

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

调用方法.nextLine()而不是.next()。我认为这应该可以解决你的问题。

#1


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.

将输入分配给String后,使用.split()方法将字符串拆分为数组。要使用.split(),请输入要将其拆分的字符。在这种情况下一个空间。例如,将此用于当前项目:.split(“”)。拆分后,您可以像任何阵列一样访问它。

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:

更新:首先使用.nextLine()并将其分配给临时字符串变量。然后你可以创建另一个扫描仪并输入一个字符串。例如:

Scanner sc = new Scanner(YOUR TEMPORARY VARIABLE);

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

您现在可以使用.next()来获取单个字符串。

#2


0  

Here is the Asgn2 class

这是Asgn2类

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

这是Player类

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 :)

在任何人去之前说发布代码块对OP没有帮助,我和他在聊天室中解释这些东西,所以不要讨厌:)

#3


-1  

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

调用方法.nextLine()而不是.next()。我认为这应该可以解决你的问题。