Results 21 to 25 of 25
- 09-10-2009, 01:12 PM #21
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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
- 09-14-2009, 06:25 AM #22
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
What is your maxLength() method doing? i think thats what its called.
might find solve in what that method is doing.
-Regards Hardo
- 09-15-2009, 03:50 AM #23
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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.
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.Java Code:int length = (maxLength(row,col,rinc,cinc) + maxLength(row,col,-rinc,-cinc) - 1); if (length == 5){ System.out.println("You have won!"); }
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
- 09-15-2009, 05:17 AM #24
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I'll do my best to help, but all the code that follows is untested and error-prone...
For your scanner...
Hopefully that solves your problem.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
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 escapedLast 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!
- 09-15-2009, 08:16 AM #25
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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 ;)
Similar Threads
-
Array question
By McChill in forum New To JavaReplies: 5Last Post: 02-20-2009, 02:18 AM -
question about writing a toString method
By heather.diggs in forum New To JavaReplies: 2Last Post: 11-12-2008, 11:13 PM -
Question about CAS (compare and swap) method
By illidian in forum Advanced JavaReplies: 1Last Post: 04-04-2008, 08:49 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM -
Return question in a method.
By MetalGear in forum New To JavaReplies: 1Last Post: 01-13-2008, 04:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks