Reputation: 297
Okay so this has been bugging me right into the early hours of the morning.
static void play(int[][] puzzle) {
int var1 = 10;
int var2 = 2;
int[][] original = new int[N][N];
reset(puzzle);
original(original);
int[][] arroriginal = original;
scramble(puzzle);
int[][] currentarr = puzzle;
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
System.out.println("---- Instructions ----");
System.out.println("To complete the puzzle, order the numbers in numerical order using the commands");
print(currentarr);
while (currentarr!=original){
int b=0;
System.out.println("");
System.out.println("For row enter 'r'");
System.out.println("For Column enter 'c'");
System.out.println("");
String input = s.nextLine().trim();
if (input.equals("r")) {
System.out.println("What row?");
int rows = s.nextInt();
rotateRow(currentarr,rows);
print(currentarr);
b++;
}
else if (input.equals("c")) {
System.out.println("What column?");
int column = s.nextInt();
rotateColumn(currentarr,column);
print(currentarr);
b++;
}
if (b == 0){
System.out.println("Invalid input");
}
boolean truefalse = Arrays.equals(arroriginal, currentarr);
if (truefalse == true){
System.out.println("Congratulations");
break;
}
}
}
This is my code - when ran this is what I get:
### Testing puzzle game play####
---- Instructions ----
To complete the puzzle, order the numbers in numerical order using the commands
0 1 2 3
7 4 5 6
11 8 9 10
12 13 14 15
For row enter 'r'
For Column enter 'c'
r
What row?
1
0 1 2 3
6 7 4 5
11 8 9 10
12 13 14 15
For row enter 'r'
For Column enter 'c'
Invalid input
For row enter 'r'
For Column enter 'c'
For some reason I get 'invalid input' when I shouldnt be and I also get a repeat of 'For row enter 'r', for column enter 'c'.
My assignment is due in 8 hours and I have literally tried everything, if you manage to fix this you must be a wizard of all things java haha.
Upvotes: 0
Views: 231
Reputation: 2953
The problem is with the s.nextInt()
command it only reads the int value. So when you continue reading with s.nextLine()
you get the "\n" Enter key. So to skip this you have to add the s.nextLine()
. Hope this should be clear now.
Try something like that:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
Upvotes: 4