Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Russo is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Its getting through ok. but when it gets to that point it doesn't prompt for an input...which has got me. If i initialise another scanner class it goes through ok..but it probably shouldnt be the solution. code below.

    Java Code:
      private void doit ()
        {
    
        // User input for value n
        Scanner console = new Scanner(System.in);
        System.out.print("Enter the number of tokens for each side:");
        int n = console.nextInt(); // read an Integer
         
        //Create the board
        Board();
    
        //Add n Stones for Black and White
        addStones(n, BLACK);
        addStones(n, WHITE);
    
        //Print the Board
        print();
    
        
    
    Scanner console2 = new Scanner(System.in);
    System.out.print("Enter board position (row col) : ");
    String input = console2.nextLine();
    
    System.out.println(input);
    //get the input and split value
    
    String[] token = input.split(" ");
    int row = Integer.valueOf(token[0]);
    int col = Integer.valueOf(token[1]);
    
    if (valid(row,col)){
    
    int rinc = 1; //to change
    int cinc = 1; //to change
    
    maxLength(row,col,rinc,cinc);
    
    
    System.out.println(row + "," + col);
    System.out.println(rinc + "," + cinc);
    }
    else{System.out.println("Invalid Board Position - Bye!");}
    
    
     
    
        //length(row,col,rinc,cinc) + length(row,col,-rinc,-cinc) - 1;
    
    
    
        }// End Method

  2. #22
    Hardo is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    What is your maxLength() method doing? i think thats what its called.
    might find solve in what that method is doing.

    -Regards Hardo

  3. #23
    Russo is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Here is what the max length statement is doing now, it was there as blank code really before (testing other things). maxLength is to determine how many tokens of the same type are together in any direction.

    Java Code:
    int length = (maxLength(row,col,rinc,cinc) + maxLength(row,col,-rinc,-cinc) - 1);
                    if (length == 5){
                        System.out.println("You have won!");
                    }
    pm me if you want to see the rest of the code. I have still had to initialise the second scanner class to get it working properly.

    Also on another topic of this, something I havent learnt in class but how can you check the validility of a string? ie. I want to check if the user has entered the string correctly in format of:

    XX XX

    where X is an integer and there is a space in the middle?

    Cheers,

    Russo

  4. #24
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I'll do my best to help, but all the code that follows is untested and error-prone...

    For your scanner...

    Java Code:
      private void doit ()
        {
    
        // User input for value n
        Scanner console = new Scanner(System.in);
        System.out.print("Enter the number of tokens for each side:");
        int n = console.nextInt(); // read an Integer
        console.nextLine();
        //skip a line... I think you are reading getting the end of the line after the int
        //the input line is really like 1000\n, so the nextInt() gets the 1000, but leaves the \n
        //so you get an empty string when it reads the nextline, because the newline (\n) is
        //next character
        //Create the board
        Board();
    
        //Add n Stones for Black and White
        addStones(n, BLACK);
        addStones(n, WHITE);
    
        //Print the Board
        print();
    
    System.out.print("Enter board position (row col) : ");
    String input = console.nextLine();
    
    System.out.println(input);
    //get the input and split value
    
    String[] token = input.split(" ");
    int row = Integer.valueOf(token[0]);
    int col = Integer.valueOf(token[1]);
    
    if (valid(row,col)){
    
    int rinc = 1; //to change
    int cinc = 1; //to change
    
    maxLength(row,col,rinc,cinc);
    
    
    System.out.println(row + "," + col);
    System.out.println(rinc + "," + cinc);
    }
    else{System.out.println("Invalid Board Position - Bye!");}
    
    
     
    
        //length(row,col,rinc,cinc) + length(row,col,-rinc,-cinc) - 1;
    
    
    
        }// End Method
    Hopefully that solves your problem.

    As for validating a string... use String.matches(String regex) and the appropriate regex.
    Java Code:
    //for your example...
    string.matches("\\d\\d \\d\\d");
    //\d is a decimal-[0-9] and the space is your space, so it would be
    //XX XX where X is a digit
    //backslashes in regexes need to be escaped
    Last edited by Singing Boyo; 09-15-2009 at 05:22 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #25
    Russo is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Hi Singing Boyo,

    That has done it! Thanks so much for your help. Thanks for the description in there to makes a lot of sense now! The regex also worked, I added a few more options with that to to allow a few valid inputs such as

    XX XX
    X X
    XX X
    X XX

    I think i've got all user input under control now ;)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM
  2. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-12-2008, 11:13 PM
  3. Question about CAS (compare and swap) method
    By illidian in forum Advanced Java
    Replies: 1
    Last Post: 04-04-2008, 08:49 PM
  4. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 02:39 AM
  5. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 04:45 AM

Posting Permissions

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