Please help me with fixing this java code: import java.io.*; import java.util.*;
ID: 3794839 • Letter: P
Question
Please help me with fixing this java code:
import java.io.*;
import java.util.*;
public class HW_practice{
private static int[][] array;
private static int row, col;
public static void readArray(){
String fileName = "example.txt";
BufferedReader br = null;
FileReader fr = null;
try{
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String currentLine;
int i = 0, r = 0, c = 0;
while ((currentLine = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(currentLine);
if(i++ == 0){
while(st.hasMoreTokens()){
row = Integer.parseInt(st.nextToken());
st.nextToken();
col = Integer.parseInt(st.nextToken());
}
array = new int[row][col];
continue;
}
c = 0;
while(st.hasMoreTokens()){
array[r][c++] = Integer.parseInt(st.nextToken());
}
r++;
}
br.close();
} catch (IOException e){
System.err.println(e);
}
}
public static void print(){
for(int[] a: array){
for(int num: a){
System.out.print(num + " ");
}
System.out.println();
}
}
public static void command(String com1, String com2){
if(com1.substring(0,1).equals("S") || com2.substring(0,1).equals("S")){
shiftCommand(com1.substring(1,3), com2.substring(1,3));
}
else if(com1.substring(0,1).equals("F") || com2.substring(0,1).equals("F")){
flipCommand(com1.substring(1,3), com2.substring(1,3));
}else{
System.out.println("Please enter valid command.");
}
}
public static void shiftCommand(String shift1, String shift2){
int num = Integer.parseInt(shift1.substring(1,3));
if(shift1.charAt(1) == 'C' || shift2.charAt(1) == 'C'){
int key = array[4][num-1];
int col = 0;
for(int i = 0; i < col; i++){
int temp = array[i][num-1];
array[i][num-1] = key;
key = temp;
}
}else{
int key = array[num-1][4];
int row = 0;
for(int i = 0; i < row; i++){
int temp = array[num-1][i];
array[num-1][i] = key;
key = temp;
}
}
}
public static void flipCommand(String flip1, String flip2){
int num = Integer.parseInt(flip1.substring(1,3));
if(flip1.charAt(1) == 'C' || flip2.charAt(1) == 'C'){
for(int i = 0; i < array.length/2; i++){
int temp = array[i][num-1];
array[i][num-1] = array[array.length-1-i][num-1];
array[array.length-1-i][num-2] = temp;
}
}else{
for(int i = 0; i < array.length/2; i++){
int temp = array[num-1][i];
array[num-1][i] = array[num-1][array.length-1-i];
array[num-1][array.length-1-i] = temp;
}
}
}
public static void main(String []args){
Scanner console = new Scanner(System.in);
System.out.println("Please enter your command: ");
String com1 = console.next();
String com2 = console.next();
console.close();
command(com1, com2);
print();
}
}
I was supposed to get the user input as following:
Please enter your command:
SC3 FR1
And got this error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.substring(String.java:1963)
at HW_practice.shiftCommand(HW_practice.java:71)
at HW_practice.command(HW_practice.java:61)
at HW_practice.main(HW_practice.java:117)
Explanation / Answer
Right. The problem with your code is that, at line 61, you tried extracting the String using the command:
int num = Integer.parseInt(shift1.substring(1,3)); //This string has only 2 characters, C3 and substring(1, 3)
Here the problem lies. When called from main(), the string is of 3 characters each. SC3, and FR1.
Where are when the same string has been passed to command(), the whole string is passed, and therefore, will have 3 characters in that function. But when coming to shiftCommand(), you're passing only 2 characters from command() to shiftCommand(), so, the string accessible in shiftCommand() is just C3, R1. And when you access that using substring(1, 3), this leads to String index out of range error. So, if you want to access only the last character as an integer i.e., 3, or 1, you should use simple substring(1) which will read string starting from index 1, till the end.
The other problem with your code is that, you declared array as a 2-D variable, but it has not been initialized, and therefore, memory is not allocated. The flow of your code is: Starting from main() it will go to command(), then to shiftCommand(), and through out these functions, you had not initialized the global variable array[][], and you're trying to access that array which has not been initialized in the shiftCommand function using the instruction: int key = array[4][num-1]; which could again lead to error, NullPointerException.
If you want your problem to be solved exactly, please also serve with the problem definition. Without that, we could not get an exact output of what you're expecting.