In: Computer Science
Problem with code. The fixed code would be greatly appreciated!
Error messages:
PlayerMoveDungeon2.java:21: error: cannot find symbol
for (i = 0; i < length; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:21: error: cannot find symbol
for (i = 0; i < length; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:21: error: cannot find symbol
for (i = 0; i < length; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:22: error: cannot find symbol
char myChar = moveChar.charAt(i);
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:32: error: cannot find symbol
for (i = 0; i < width; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:32: error: cannot find symbol
for (i = 0; i < width; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:32: error: cannot find symbol
for (i = 0; i < width; i++) {
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:33: error: cannot find symbol
myChar = moveChar.chatAt(i);
^
symbol: variable i
location: class PlayerMoveDungeon2
PlayerMoveDungeon2.java:45: error: no suitable method found for
println(int,int)
System.out.println(x, y);
^
method PrintStream.println() is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char[]) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(String) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(Object) is not applicable
(actual and formal argument lists differ in length)
9 errors
Code:
import java.util.Scanner;
public class PlayerMoveDungeon2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int cases = input.nextInt();
int length = 0;
int width = 0;
int x = 0;
int y = 0;
for (int i = 0; i < cases; i++) {
length = input.nextInt();
width = input.nextInt();
}
{
x = input.nextInt();
y = input.nextInt();
}
String moveChar = input.nextLine();
moveChar.replaceAll(" ", "");
for (i = 0; i < length; i++) {
char myChar = moveChar.charAt(i);
if (myChar == 'W' || myChar == 'w') {
if (y > 0) {
y--;
}
}
if (myChar == 'S' || myChar == 's') {
if (y < length)
y++;
}
for (i = 0; i < width; i++) {
myChar = moveChar.chatAt(i);
if (myChar == 'A' || myChar == 'a') {
if (x > 0) {
x--;
}
}
if (myChar == 'D' || myChar == 'd') {
if (x < width) {
x++;
}
}
}
System.out.println(x, y);
}
}
}
Program:
import java.util.Scanner;
public class PlayerMoveDungeon2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int cases = input.nextInt();
int length = 0;
int width = 0;
int x = 0;
int y = 0;
int i; // Initializing variable i since you have been using it for other loops
for (i = 0; i < cases; i++) {
length = input.nextInt();
width = input.nextInt();
}
{
x = input.nextInt();
y = input.nextInt();
}
String moveChar = input.nextLine();
moveChar.replaceAll(" ", "");
for (i = 0; i < length; i++) {
char myChar = moveChar.charAt(i);
if (myChar == 'W' || myChar == 'w') {
if (y > 0) {
y--;
}
}
if (myChar == 'S' || myChar == 's') {
if (y < length)
y++;
}
for (i = 0; i < width; i++) {
myChar = moveChar.charAt(i); // It's typo mistake charAt() is the function name
if (myChar == 'A' || myChar == 'a') {
if (x > 0) {
x--;
}
}
if (myChar == 'D' || myChar == 'd') {
if (x < width) {
x++;
}
}
}
System.out.println(x+ " "+y); // Use + to print multiple results
}
}
}
Note: Please find the comments in the code