Exception in thread main ArrayIndexOutOfBoundsException - java

Can someone maybe help me with the following error. Exception in thread main java.lang.arrayindexoutofboundsexception : 3 at RowTrans.encrypt(Rowtrans.java:33)
at RowTrans.main(Rowtrans.java :7)
In my program I want to get a text. Put it in a matrix with 5 columns and determine the rows according to the length of the text. Then i want to change the column and row position so that the row gets the columns position and the column the row. And when a row does not contain 5 values I want to add the character Z in the empty spaces. Can anyone assist me on this error please.
Here is my code
import java.util.Scanner;
public class ColTrans {
public static void main(String[] args)
{
String ori = "This is my horse";
String enc = encrypt(ori);
System.out.println(enc);
// String dec = decrypt(enc);
// System.out.println(dec);
}
static String encrypt(String text)
{
String result = "";
text = text.toUpperCase();
int length = text.length();
int rows = length / 5;
char[][] b = new char[rows][5];
char[][] c = new char[5][rows];
char[] d = new char[length];
if ((length % 5) != 0)
rows = rows + 1;
int k = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < 5; j++)
{
if (k > length)
b[i][j] = 'Z';
else
{
d[k] = text.charAt(k);
b[i][j] = d[k];
}
k++;
}
for (int i = 0; i < 5; i++)
for (int j = 0; j < rows; j++)
{
c[i][j] = b[j][i];
result = result + c[i][j];
}
return result;
}
}

Here is the cause:
You are increamenting row variable by one, once you have defined the array.
Move following line before line char [][] b =new char[rows][5];
if ((length % 5) != 0)
rows = rows + 1;

There are 2 issues in your code. First move the mod part before the matrix instantiation :
if ((length % 5) != 0)
rows = rows + 1;
char [][] b =new char[rows][5];
[...]
Then change if ( k > length ) to if ( k >= length )

just change your code as following:
if ((length % 5) != 0)
rows = rows + 1;
char[][] b = new char[rows][5];
char[][] c = new char[5][rows];
char[] d = new char[length];

According to your description:
text = text.toUpperCase();
char[] b = text.toCharArray();
char[][] c = new char[b.length][5];
int bLen = 0;
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < 5; j++) {
if(bLen < b.length)
c[i][j] = b[bLen++];
else
c[i][j] = 'Z';
}
}
//change the column and row position
char[][]d = new char[c[0].length][c.length];
for (int i = 0; i < d.length; i++) {
for (int j = 0; j < d[0].length; j++) {
d[i][j] = c[j][i];
}
}
Output: TI EZZZZZZZZZZZZHSHZZZZZZZZZZZZZI OZZZZZZZZZZZZZSMRZZZZZZZZZZZZZ YSZZZZZZZZZZZZZ

Related

All the possible path to reach bottom of a m*n matrix

public static int printStepsToReachBottom(int rows, int columns, String[] array) {
if (rows == 1) {
array[0] = "";
for (int i = 0; i < columns - 1; i++) {
array[0] += "H";
}
return 1;
}
if (columns == 1) {
array[0] = "";
for (int i = 0; i < rows - 1; i++) {
array[0] += "V";
}
return 1;
}
String[] temporary = new String[1000];
int k = 0;
int firstTypeMove = printStepsToReachBottom(rows - 1, columns, array);
for (int i = 0; i < firstTypeMove; i++) {
temporary[k] = array[i] + "V";
k++;
}
int secondTypeMove = printStepsToReachBottom(rows, columns - 1, array);
for (int i = 0; i < secondTypeMove; i++) {
temporary[k] = array[i] + "H";
k++;
}
for (int i = 0; i < secondTypeMove + firstTypeMove; i++) {
array[i] = temporary[i];
}
return secondTypeMove + firstTypeMove;
}
public static void main(String[] args) {
String[] array = new String[1000];
int outputSize = printStepsToReachBottom(2, 2, array);
for (int i = 0; i < outputSize; i++) {
System.out.println(array[i]);
}
}
I can't figure out how this code snippet is working. I didn't understand the logic. It prints All the possible paths to reach the bottom of an m*n matrix
It prints "HV" and "VH" for the 2x2 matrix. Help me.
You can breakdown the code into three parts;
if (rows == 1) {
array[0] = "";
for (int i = 0; i < columns - 1; i++) {
array[0] += "H";
}
return 1;
}
if (columns == 1) {
array[0] = "";
for (int i = 0; i < rows - 1; i++) {
array[0] += "V";
}
return 1;
}
This part is the end case of the recursion. It says that there is no more rows or columns to go and return an array with size 1 either containing H(or H's) or V(or V's)
String[] temporary = new String[1000];
int k = 0;
int firstTypeMove = printStepsToReachBottom(rows - 1, columns, array);
for (int i = 0; i < firstTypeMove; i++) {
temporary[k] = array[i] + "V";
k++;
}
int secondTypeMove = printStepsToReachBottom(rows, columns - 1, array);
for (int i = 0; i < secondTypeMove; i++) {
temporary[k] = array[i] + "H";
k++;
}
The second part executes the recursion through both H and V directions for any given step which adds two more recursive calls to the stack (Although, in execution it performs a depth-first search rather than a breadth-first one, the idea is easier to grasp that way)
int secondTypeMove = printStepsToReachBottom(rows, columns - 1, array);
for (int i = 0; i < secondTypeMove; i++) {
temporary[k] = array[i] + "H";
k++;
}
for (int i = 0; i < secondTypeMove + firstTypeMove; i++) {
array[i] = temporary[i];
}
return secondTypeMove + firstTypeMove;
And the last part collects the outputs from both H and V directions into the global array and returns the number of outputs to the upper stack.
Here is a simpler recursive Depth First Search that will do the same:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
dfsPrintAllPathesTopToBottom(3,3);
}
//performs dfs to map all possible paths on a rows x columns matrix
//from top left to bottom right by moving right (R) or down (R)
public static void dfsPrintAllPathesTopToBottom(int rows, int columns){
List<String> path = new ArrayList<>();
dfsPrintAllPathesTopToBottom(0, 0,rows,columns,path);
}
public static void dfsPrintAllPathesTopToBottom(int row, int col, int rows, int columns, List<String> path ){
if(row == rows -1 && col == columns -1){//bottom left reached
System.out.println(path);
return;
}
//move right
int newCol = col +1;
if(newCol < columns ){
List<String>newPath = new ArrayList<>(path);
newPath.add("R");//or newPath.add("H")
dfsPrintAllPathesTopToBottom(row, newCol, rows, columns, newPath);
}
//move down
int newRow = row +1;
if(newRow < rows ){
List<String>newPath = new ArrayList<>(path);
newPath.add("D"); //or newPath.add("V")
dfsPrintAllPathesTopToBottom(newRow, col, rows, columns, new ArrayList<>(newPath));
}
}
}

Integer variable does not update when if condition is true

public class test
{
static Scanner store = new Scanner(System.in);
public static void main(String[] args)
{
String str1 = args[0];
String str2 = args[1];
System.out.printf("%nThere are %d dissimilar characters in the two strings.%n", CountNotSim(str1, str2));
}
public static int CountNotSim(String str1, String str2)
{
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
char[] a1 = new char[s1.length()];
char[] a2 = new char[s2.length()];
for (int g = 0; g < s1.length(); g++)
a1[g] = s1.charAt(g);
for (int h = 0; h < s2.length(); h++)
a2[h] = s2.charAt(h);
int check = 0, stored;
char[] array = new char[26];
int ctr = s1.length() + s2.length();
for (int i = 0; i < a1.length; i++)
{
check = 0;
stored = 0;
for (int j = 0; j < a2.length; j++)
{
if (a1[i] == a2[j])
{
check++;
for (int k = 0; k < 26; k++)
{
if (array[k] == ' ')
if (stored == 0)
array[k] = a1[i];
if (a1[i] == array[k])
{
stored = 1;
break;
}
}
System.out.print(stored + "/ ");
}
}
if (check > 0)
{
if (stored == 0)
ctr -= (check + 1);
else if (stored == 1)
ctr--;
}
System.out.print(ctr + " "); //checker
}
System.out.println();
return ctr;
}
}
The program checks for dissimilar letters in two strings inputted from the command line. Variable "stored" is supposed to change to 1 whenever there's a match to avoid extra deductions to variable "ctr". However, for some reason, not only does "stored's" value not change, the array "array" also doesn't update its elements whenever there's a match. I'm at a loss on how to fix it--nothing looks incorrect.
You wrote this:
char[] array = new char[26];
...
for (int k = 0; k < 26; k++)
{
if (array[k] == ' ') {
...
But you simply set the length of array not its content.
As a char array, it's filled with the default char value, which is not the character space but the value 0 (not the character 0, but the numeric value 0)
So array[k] == ' ' will never be true.
Try with that:
for (int k = 0; k < 26; k++)
{
if (array[k] == 0) {
...

Creating an object class with elements of varying but set length

I want to create an object (Wine) that contains a set of data where each data element is a different size (year, region, maker, variety, etc.). I want to be able to print these out in a tabular format using the set length of each element. I've searched and tried lots of ways but can't find the answer.
my class definition looks like this:
public class Wine {
char[] year;
char[] area; // Wine area
char[] brand; // Wine brand
char type; // R or W or F
char[] variety;
char bin;
int numbottles;
I can read data in from a txt file but the elements are shortened to the text content plus a space so the tabulation is lost.
I've tried this in my main section without success:
wine.year = rightpad(tokens[0].toCharArray(), 5) ;
wine.area = rightpad(tokens[1].toCharArray(), 9);
....
static char[] rightpad(char[] text, int leng) {
for(int i = text.length - 1; i < leng; i++){
text = (new String(text) + ' ').toCharArray();
}
return text;
}
Any suggestions would be greatly appreciated.
Chris A
You can use String.format
String.format("%-8s", "test") // "test "
You set the length of the String and use a negative value to add the space to the right (without the minus, to the left)
Complete information about the format are available in Formatter - Format String Syntax
Use org.apache.commons.lang.StringUtils Apache Commons Lang
StringUtils.leftPad("text", 7) // return " text"
Here is my parse routine which reads through the String line and pulls out each Object element then adds them to the Object at the end:
static List<Wine> parseLine(String line, List<Wine> wineInfo) {
String year = null;
String area = null;
String brand = null;
String type = null;
String variety = null;
String bin = null;
String numbottles = null;
String drinkyear = null;
String cost = null;
Wine wine = new Wine(year, area, brand, type, variety, bin,
numbottles, drinkyear, cost);
int length = line.length();
int j = 0;
char yr[] = new char[3];
for (int i = 0; i < 3; i++) {
yr[j] = line.charAt(i);
year = new String(yr);
j = j + 1;
}
char temp[] = new char[7];
j = 0;
for (int i = 3; i < 10; i++) {
temp[j] = line.charAt(i);
area = new String(temp);
j = j + 1;
}
char temp1[] = new char[12];
j = 0;
for (int i = 10; i < 22; i++) {
temp1[j] = line.charAt(i);
brand = new String(temp1);
j = j + 1;
}
char temp3[] = new char[2];
j = 0;
for (int i = 22; i < 24; i++) {
temp3[j] = line.charAt(i);
type = new String(temp3);
j = j + 1;
}
char temp2[] = new char[21];
j = 0;
for (int i = 24; i < 45; i++) {
temp2[j] = line.charAt(i);
variety = new String(temp2);
j = j + 1;
}
char tmp[] = new char[6];
j = 0;
for (int i = 45; i < 51; i++) {
tmp[j] = line.charAt(i);
bin = new String(tmp);
j = j + 1;
}
char temp4[] = new char[3];
j = 0;
for (int i = 51; i < 54; i++) {
temp4[j] = line.charAt(i);
numbottles = new String(temp4);
j = j + 1;
}
char temp5[] = new char[4];
j = 0;
for (int i = 54; i < 58; i++) {
temp5[j] = line.charAt(i);
drinkyear = new String(temp5);
j = j + 1;
}
char temp6[] = new char[7];
j = 0;
for (int i = 58; i < length; i++) {
temp6[j] = line.charAt(i);
cost = new String(temp6);
j = j + 1;
}
wine.setYear(year);
wine.setArea(area);
wine.setBrand(brand);
wine.setType(type);
wine.setVariety(variety);
wine.setBin(bin);
wine.setNumbottles(numbottles);
wine.setDrinkYear(drinkyear);
wine.setCost(cost);
wineInfo.add(new Wine(year, area, brand, type, variety, bin,
numbottles, drinkyear, cost));
return wineInfo;
}
}
I then used a collections Comparator method to sort:
List<Wine> wineByYear = new ArrayList<Wine>(wineInfo);
Collections.sort(wineByYear, Wine.YearComparator);

Java 2D Array with Strings, a bug and need to add a simple feature

So I'm developing a simple program that asks the user how many columns and lines it wants, and then generates a string with the afformentioned size, here's the code (adapted from what some kind developer made to answer another question)
String[][] grid = new String[int1][int2];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
StringBuilder sb = new StringBuilder(int1);
for (int j = 0; j < grid[i].length; j++) {
sb.append(AB.charAt(rnd.nextInt(AB.length())));
sb.toString();
grid[i][j] = sb.toString();
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(""+grid[i][j]);
}
System.out.println();
}
}
Now, this has a bug I can't seem to fix, I collect the wanted size through int1 and int2 (int1 being rows and int2 being columns), however the output respects the number of rows (int2) but doesn't seem to respect the column number. Here's an example, if I define int1 (rows) = 3 and int2 (columns) = 4 here's the output:
_____W__W_
_____W__W_
_____W__WW
As you can see, there's 3 rows but 10 columns, why is that? How do I fix it?
I also need to implement a simple addon that I can't seem to get my head around. In the first row, there needs to be (in a random position, just has to be the first row) the char 'S' and in the last row there needs to be the char 'E' (also in a random position). They can't add to the size of their respective columns, so they need to replace a '_' or 'W'. Any help? :D
Your problem is that you don't clear the StringBuilder. in the inner loop. so first time you write 1 char, than 2 chars and 3 and 4 (which totals to 10)
You can fix it like that:
String[][] grid = new String[int1][int2];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
StringBuilder sb = new StringBuilder(int1);
for (int j = 0; j < grid[i].length; j++) {
sb.setLenght(0); // <-- To reset the data
sb.append(AB.charAt(rnd.nextInt(AB.length())));
sb.toString(); // You can delete this line which does nothing.
grid[i][j] = sb.toString();
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(""+grid[i][j]);
}
System.out.println();
}
But I would avoid the SB at all... and with the S and E
String[][] grid = new String[int1][int2];
String AB = "_W";
SecureRandom rnd = new SecureRandom();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = "" + AB.charAt(rnd.nextInt(AB.length()));
}
}
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(""+grid[i][j]);
}
System.out.println();
}
grid[0][rnd.nextInt(int2)] = "S";
grid[int1-1][rnd.nextInt(int2)] = "E";

Trying to read matrix in Java

Hi guys im trying to read from input a number that determines the size of matrix to be created. Then read said matrix and reproduce it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i < dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
Thing is it is ignoring the last char of eache line and the last line. Testing with this input:
4
XXXX
OOO.
....
....
Your first double for-loop (the one that reads the input into your tab-matrix) should say i < dim instead of i < dim - 1, and idem for jin the inner for-loop
Like this:
for (int i = 0; i < dim ; i++) { //Removed - 1
for (int j = 0; j < dim ; j++) { //Removed - 1
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
you just had a small issue you had to go to the last dimension so for an array of 5 you should go til 4 but in the code it goes just to 3 because of the int i = 0; **i < dim - 1**; i++
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int dim = in.nextInt();
char[][] tab = new char[dim][dim];
in.nextLine();
String temp = in.nextLine();
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j < dim - 1; j++) {
tab[i][j] = temp.charAt(j);
}
temp = in.nextLine();
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
System.out.print(tab[i][j]);
}
System.out.println();
}
}
The bounds on the loops when creating your matrix are incorrect. You should either do
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
Or
for (int i = 0; i <= dim - 1; i++) {
for (int j = 0; j <= dim - 1; j++) {
But don't mix the two. Pick either strictly less than a bound or less than or equal to the bound - 1.

Categories