I'm trying to read a pdf file using pdfbox line by line so that i can compare it with an older version.
For this, i have done:
static String[][] SplitString_into_array(String string_to_split)
{
final int SIZE = 1495; //max size allowed in array
int number_arrays = string_to_split.length()/SIZE;
int start = 0;
int end = SIZE;
int max = string_to_split.length();
int i, j;
// Pdfs are very big, so i had to create a 2d array,
// where ROWS are the number of arrays to be created
// based on the size and COLS are the max size allowed.
String[][] pdf_final = new String[number_arrays][SIZE];
String[] pdf_split = new String[SIZE];
for(i = 0; i < pdf_final.length; i++)
{
String tmp = string_to_split.substring(start, end);
for(j = 0; j < pdf_final[i].length-1; j++)
{
pdf_split = tmp.split("\\r?\\n");
pdf_final[i][j] = pdf_split[j];
}
start = SIZE + 1;
end = SIZE + SIZE;
if(end > max)
{
end = max;
}
}
return pdf_final;
}
The problem is that i'm getting an error out of bound exception when doing:
pdf_final[i][j] = pdf_split[j];
It seems that J is only reaching the max size of I, but i have no idea why, since both sizes are defined correctly.
Can someone help me?
Regards
pdf_final[i].length can exceed pdf_split.length if your initial string is big enough. To be more exact: when string_to_split.length()/SIZE > SIZE.
Im sorry above answer is not true. It could be that i gets out of bound and not j for big files.
Like Jon Skeet said the statement doesn't give you an array of size 1495:
pdf_split = tmp.split("\\r?\\n")
Related
I am pretty new to java and am just learning 2D arrays. I am trying to get the top 5 numbers to display from a random list. I think this could work but am not sure why I am getting an error. One other thing is that I cannot use the sort function.
Code here:
public static void main(String[] args) {
//Random Number stuff
Random rand = new Random();
int[] large = new int [5];
int max = 0, index;
int[][] arrSize = new int [4][5];
for (int i = 0; i < arrSize.length; i++) {
for (int j=0; j< arrSize[i].length; j++) {
arrSize[i][j] = rand.nextInt(89) + 10;
System.out.print(arrSize[i][j] + " ");
}
System.out.println();
}
// Top 5
for (int p = 0; p < 5; p++) {
max = arrSize [0][0];
index = 0;
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize[i].length; j++) {
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
}
}
large[p] = max;
arrSize[index] = Integer.MIN_VALUE; //Error here
System.out.println("Highest Number: " + large[p]);
}
}
}
Error text:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to int[]
at secondAssignment.BiggestNumbersRectangular.main(BiggestNumbersRectangular.java:47)
I am not sure why I am getting an error, any help would appreciated. If anyone else has any answers for how I could get the top 5 in a different way that would also be appreciated.
You declare your arrSize here
int[][] arrSize = new int [4][5];
and try to set it's value here
arrSize[index] = Integer.MIN_VALUE;
The Object at arrSize[index] is an array.
Remember that a 2D array basically looks like this:
arrSize
- arrSize[0]
- arrSize[0][0]
- arrSize[0][1]
- arrSize[1]
- arrSize[1][0]
- arrSize[1][1]
- arrSize[2]
- arrSize[2][0]
- arrSize[2][1]
- arrSize[3]
- arrSize[3][0]
- arrSize[3][1]
Because index is a single int, you are assentially calling arrSize[0], which contains arrSize[0][0] and arrSize[0][1].
The Integer.MIN_VALUE is not an array of integers. It is an int. You cannot assign int to int[].
As you can see in other parts of the code, to access values in 2D array arrSize, you need 2 indexes in your case (and usually) i and j.
You need to save both i and j after you find the highest number.
if (max < arrSize[i][j]) {
max = arrSize[i][j];
indexI = i;
indexJ = j;
}
and then
arrSize[indexI][indexJ] = Integer.MIN_VALUE;
As to why you got the error, arrSize[i] gets you a 1D array. It's still an array and you cannot set an array to an integer (Integer.MIN_VALUE). in Java represented as int[] in the error message.
The algorithm could be improved, instead of using a single integer max for saving the highest value, you could use a maxArr of the same size as the number of highest numbers you want (in your case 5) and check against all of the numbers in maxArr using a for in place of
if (max < arrSize[i][j]) {
max = arrSize[i][j];
index = i;
}
That would mean you could remove the index (or indexI and indexJ) and topmost for cycle (for (int p = 0; p < 5; p++)). But that's another topic, and one you should learn yourself.
I'm new to coding and have been set a challenge of creating an method that prints out a multiplication array.
My code is as follows...
public class TimesTableArray
{
public static void main(String[] args)
{
int size = 12;
int number = 3;
}
private static int [] getTimesTable(int size, int number)
{
int[] timesTable = new int[size];
for (int i = 0; i < size; i++)
{
timesTable[i] = number * (i +1);
}
return timesTable;
}
}
I get the following output...
3
6
9
12
15
18
21
24
27
30
33
0
I need the 12th item to read 36, not 0. I know it's to do with the index reads from 0 to 11, but I don't know how to amend my code to make this so.
Please could someone guide me on how to do this?
You have a classic "off by one" error.
If you start at 1, then you INCLUDE the size, and have to subtract one to get the Index:
for (int i = 1; i <= size; i++)
{
timesTable[i - 1] = number * i;
}
If you start at 0, then you EXCLUDE the size, and have to add one when you multiply:
for (int i = 0; i < size; i++)
{
timesTable[i] = number * (i + 1);
}
You're going to have people swear up and down that one is correct and the other is wrong. This is down to preference. Do what "makes sense" to you.
In the population loop, you are iterating from 1 rather than 0 but you do not offset the terminal condition. I would rewrite you loop to follow the standard pattern rather than trying to bake in the offset. So...
for (int index = 0 ; index < size ; ++index) { ... }
This will allow the index to line up naturally in the assignment. Apply the offset in the calcuation.
timesTable[index] = number * (index + 1);
So you get...
private static int[] getTimesTable(int size, int number) {
int[] timesTable = new int[size];
for (int i = 0; i < size; i++) {
timesTable[i] = number * (i+1);
}
return timesTable;
}
You are having an off-by-one error. Since you want to have an array of 12 answers, starting with 1, your loop range has to be from 1 to 12 inclusive. Therefore:
private static int [] getTimesTable(int size, int number)
{
int[] timesTable = new int[size];
for (int i = 1; i <= (size); i++)
{
timesTable[i-1] = number * i;
}
return timesTable;
}
The reason why your last index was zero, is because numeric array indexes are initialized to zero after instantiation (new int[size]).
I have this bubblesort code that i'm performing a runtime analysis on recording the time it takes to sort the array. I was wondering if there is any way i could increment the size of the array using a loop? Because at the moment i am incrementing it 100 at a time manually and i need to reach an array size of 5000.
public class BubbleSortworking{
public static void main (String[] args) {
Random rand = new Random();
int myArray[] = new int[100]; //How to increment this using a loop
int count, count2;
count2 = 2; //amount of times to run the loop
//repeats the bubble sort, while also producing new arrays each time
for (count = 0; count < count2; count++){
for (int i = 0; i < myArray.length; i++){
myArray[i] = rand.nextInt(100) + 1; //produce numbers between 1 - ?
//System.out.print(myArray[i] + ", "); //displays unsorted array
}
bubble(myArray);
// uncomment below 2 lines to prove each new sorted array cycle is unique
//for (int i = 0; i < myArray.length; i++)
// System.out.print(myArray[i] + ", ");
}
}
public static void bubble(int myArray[]){
int temp;
long start = System.nanoTime();
//System.out.println("start " + start);
//for (count = 0; count < count2; count++){
for (int i=0; i < myArray.length - 1; i++) {
for(int j=myArray.length - 1; j > i; j--) {
if (myArray[j] < myArray[j-1]){
temp = myArray[j];
myArray[j] = myArray[j-1];
myArray[j-1] = temp;
}
}
}
long end = System.nanoTime();
System.out.println(end - start);
//System.out.println("elapsed time " + (end - start));
}
}
No you can't change the size of an array once created. You either have to allocate it bigger than you think you'll need or accept the overhead of having to reallocate it needs to grow in size. When it does you'll have to allocate a new one and copy the data from the old to the new.
You either need to use an ArrayList, which will do this for you but with extra overheads.
Or, you can allocate the array as size 5000 before you start, and record up to how many elements you have used so far in a variable (rather than relying on array.length)
Or, you can resize the array by making a new array which is bigger and copying all the elements to it (System.arrayCopy(..)), as well as putting the new ones in.
The answer by rizon is correct, you cannot change the size of an array. BTW, nowhere are you re-creating the array, nor do I see where you are processing 5000 elements. If you are concerned about processing time, you would not want to recreate/resize an array, as that would be very inefficient. You would want a different solution.
This may help you
int[] intA = new int[100];
int sizeToIncrement = 100;
for(int i=0;i<5000;i++) {
if(i== intA.length ) {
intA = Arrays.copyOf(intA, intA.length + sizeToIncrement);
}
intA[i] = i;
}
Basically, I have an assignment that requires me to find the mode of a given set of numbers.
This is my Method:
public void findMode (){
/* The vector data is analyzed and transferred into a smaller vector
smallList (0..100). For each occurrence of n in vector data,
smallList[n] is incremented +1. function Largest is then called
to find the largest quantity in vector smallList. The mode(s)
is/are printed out. */
int loop, largest;
int[] smallList = new int[101];
for (int i = 0; i < myHowMany; i++)
{
smallList[myData[i]]++;
}
int max = 0;
for (int i = 0; i < smallList.length; i++)
{
if (max < smallList[i])
{
max = smallList[i];
}
}
//Max is 26
int size = 0;
for (int i = 0; i < smallList.length; i++)
{
if (i == max) size++;
}
int[] modes = new int[size];
int modeIndex = 0;
for (int i = 0; i < smallList.length; i++)
{
if (smallList[i] == max)
{
modes[modeIndex] = smallList[i];
System.out.println(modes[modeIndex]);
modeIndex++;
}
} Everything compiles fine, but when I run this method, I get an out of bounds array method. I have no idea WHY this happens so I
need to know if the community can help me
.
Solved!
Please tell me if I need more information!
edit: I forgot to mention that I get the error here:
modes[modeIndex] = smallList[i];
New Problem:
I fixed the problem from before, but now, I find that my max goes unto the array so that modes = 26(max)
Your error is in this line
if (i == max) size++;
It should be
if (smallList[i] == max) size++;
This is causing the size of modes to be wrong
That should be clear enough: either modeIndex or i exceeds the array size. Since you're looping over smallList with smallList.length, I guess the error is in the modeIndex then. In this case, size (which is used to construct modes) isn't big enough.
if (i == max) size++;
and then
if (smallList[i] == max)
Please check your value for size.
I don't know but for some reason an array refuses to change and i have no idea why. see further comments below
public static void contour (int[ ][ ][]image, int rows, int cols, int maxIntensity, int[ ][ ][]newImage) throws Exception
{
PrintWriter contour = new PrintWriter("contour.ppm");
int r = 0;
int c = 0;
int a = 0;
int sumk = 0;
while (r < rows && c < cols)
{
while (a < 3)
{
image[0][r][a] = newImage[0][r][a];
image[cols-1][r][a] = newImage[cols-1][r][a];
image[c][0][a] = newImage[c][0][a];
image[c][rows-1][a] = newImage[c][rows-1][a];
a++;
}
r++;
c++;
}
System.out.println (image[32][0][2]);
System.out.println (newImage[32][0][2]);
}
This code is a bit out of context, but you should be able to see which values I want to make the same in both arrays. The print statements are for testing purposes, and for some reason i get two different values. These arrays both have values assigned to them, but the image array will not change (and even when I create a new array in this method the same problem persists).
You can see that I define these arrays in the main method and they are as follows:
int image [][][] = readimage (fname, descriptor);
int newImage [][][] = new int [rows][cols][3];
So the image array is a returned array from a different method.
Am I overlooking something extremely obvious or what? I have been struggling with this for quite some time, so all hints, tips and explanations are greatly appreciated!!
Assuming you are traversing the columns and rows in the way you intend to...
You never reset the a variable to 0, so your code only works once.
int r = 0;
int c = 0;
int a = 0;
int sumk = 0;
while (r < rows && c < cols)
{
while (a < 3)
{
image[0][r][a] = newImage[0][r][a];
image[cols-1][r][a] = newImage[cols-1][r][a];
image[c][0][a] = newImage[c][0][a];
image[c][rows-1][a] = newImage[c][rows-1][a];
a++;
}
r++;
c++;
a=0; //<----ADD THIS
}
If either the rows parameter or the cols parameter is less than 33, then the location printed in your test will not be copied from newimage to image. The while (r < rows && c < cols) test causes the loop to terminate when the first of either r or c reaches its maximum value.