Runtime Error
Moja Azimi · 2015-05-12 17:40:49
Hello my code running prefect in my pc but in ShareCode runtime error. I'm very confused. please help me
Sirwan · 2015-05-14 17:25:45
Hi, probably you wanna reach a NEGATIVE-INDEX of the Array in your code, take a look at the indices and make sure this is correct.
AMiR · 2015-05-14 18:17:09
And also pay more attention to the input/output format. I suggest to create a testcase with more than one input block and test your app against it.
Moja Azimi · 2015-05-14 18:22:25
Tanx Sivan and AMiR. my code : import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, test, g, b;
test = input.nextInt();
input.nextLine();
number = input.nextInt();
input.nextLine();
while (test-- > 0) {
String str[] = new String[number];
for (int i = 0; i < number; i++) {
str[i] = input.nextLine();
for (String part : str[i].split(" "))
System.out.print(new StringBuilder(part).reverse().toString() + " ");
System.out.println("\b");
}
if (test == 1)
System.out.println();
}
}
}
AMiR · 2015-05-14 19:23:15
I modified your code a little bit.
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, test, g, b;
test = input.nextInt();
input.nextLine();
while (test-- > 0) {
// YOU NEED TO READ THE NUMBER OF LINES FOR EACH INPUT BLOCK
number = input.nextInt();
input.nextLine();
String str[] = new String[number];
for (int i = 0; i < number; i++) {
str[i] = input.nextLine();
int first = 1;
for (String part : str[i].split(" ")) {
// PRINTING `\b` IS NOT SAFE. TRY TO MANAGE PRINTING THE SPACE CHARACTER
if (first == 1) first = 0;
else System.out.print(" ");
System.out.print(new StringBuilder(part).reverse().toString());
}
System.out.println();
}
// YOU NEED TO PRINT A BLANK LINE BETWEEN EACH TEST BLOCK
if (test != 0)
System.out.println();
}
}
}
```
Moja Azimi · 2015-05-14 22:15:41
tanx AMiR.
pages:
- 1