CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Declaring multiple string variables

    I was hoping someone could help me to declaring multiple string variables. I tried something like this:
    Code:
    System.out.print("Enter your move (U/D/L/R)>");		
    		String movePlayer= "R", "L", "D", "U";
    		
    		movePlayer = move.next();
    		int x=0, y=0;
    		
    		if(movePlayer == "R") {
    			grid[y][x-1]='.';
    			x--;
    The part starting at: String movePlayer= "R", "L", "D", "U";

    It won't let me declare them that way. Im not sure what to do.
    I'm trying to validate the other letters so I can use them in "if statements" to make a player move in a game. Thank you for the help in advance.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Declaring multiple string variables

    Do you mean you want to declare an array of Strings?

    Your code is confusing, why initialise movePlayer to any value when on the next line you set it to the the value returned by move.next().

    BTW when comparing String values you need to use the equals() method and not ==.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Feb 2012
    Posts
    3

    Re: Declaring multiple string variables

    I guess I would want to declare an array of Strings. As far as the movePlayer variable, I was trying to tell java that I want the values to be U, D, L, R input commands scanned in from the user. Pretty much what I want the program to do is when the user types in "U+(Enter)" the player will move up one spot, and so on and so forth for the other commands.

  4. #4
    Join Date
    Feb 2012
    Posts
    3

    Re: Declaring multiple string variables

    Here's my what I have for my program so far:

    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Adventure {
    	
    	public static void main(String[] args) {
    		// Create 2D array for game board.
    		char grid[][]= new char[10][10];
    		Scanner move = new Scanner(System.in);
    		System.out.println("Here is the current game board:");
    		System.out.println("-------------------------------");
    		
    		for(int i=0; i<grid.length; i++) {			
    			for(int j=0; j<grid.length; j++) {
    				double Random = Math.random();
    				if(Random <=.05) {
    					grid[i][j]='*';
    				}
    				else if(Random > .06 && Random <= .15) {
    					grid[i][j]='X';
    				}			
    				else {
    					grid[i][j]='.';
    				}				
    				grid[0][0]='P';
    				grid[9][9]='T';
    				System.out.print(grid[i][j]);
    			}
    			System.out.println("");									
    		}				
    		
    		System.out.print("Enter your move (U/D/L/R)>");		
    		String movePlayer= "R";
    		
    		movePlayer = move.next();
    		int x=0, y=0;
    		
    		if(movePlayer == "R") {
    			grid[y][x]='.';
    			x--;
    		}
    		//if(movePlayer == "L") {
    			//grid[y][x]='.';
    			//x++;
    		//}
    		//if(movePlayer == "D") {
    			//grid[y][x]='.';
    			//y++;
    		//}
    		//if(movePlayer == "U") {
    			//grid[y][x]='.';
    			//y--;
    		//}
    		//if(grid[y][x]=='*') {
    			//System.out.println("You fell in a pit. Game Over.");
    		//}
    		//if(grid[y][x]=='X') {
    			//System.out.println("That spot is blocked. Please enter another move.");
    		//}
    		else {
    			grid[y][x]='P';
    		}
    		System.out.print(grid[y][x]);
    	}
    }
    I commented out the if statements towards the end so I can run the program without it throwing me errors.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Declaring multiple string variables

    I guess I would want to declare an array of Strings
    To create a string array and initialise it in one go do:
    Code:
    String[] movePlayer= new String[]{"R", "L", "D", "U"};
    As far as the movePlayer variable, I was trying to tell java that I want the values to be U, D, L, R input commands scanned in from the user.
    It doesn't do that.
    If you only want to handle those 4 characters then you don't need the array, just check if the input is one of the 4 characters and take the appropriate action. Remember is use equals() or equalsIgnoreCase() to do the test.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured