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
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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
