I'm looking for the equivalent word in the database by the ContextQuery method, and when a equivalent word is null the program must try to use the next index from words and add it up to the current to make it a two word, if the two word is still null the program will make it a three word looking for the next 2 value, the equivalent is now being printed in console but i have the error IndexOutOfBoundsExpection after running
for (int i = 0; i < words.size(); i++){
temp = QueryWithContext.query(words.get(i));
if((temp == null || temp.isEmpty()) && words.size() >= i+1)
{
QueryWithContext.query(words.get(i)+" "+words.get(i+1));
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1));
System.out.println("1st if");
if((temp == null || temp.isEmpty()) && words.size() >= i+2)
{
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
}
else
{
temp = words.get(i);
}
}
System.out.println(temp);
if((temp == null || temp.isEmpty()) && words.size() >= i+1)
must be
if((temp == null || temp.isEmpty()) && words.size() > i+1)
otherwise
words.get(i+1)
throws the IndexOutOfBoundsExpection.
The problem is most likely in this line: temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));. You are looping until i is less than the size of words, so i will range from 0 to n - 1.
The problem is that in your code, you keep going till i + 2 (and previously, i + 1). This is what is most likely causing your error. To fix this, see if you can do the following: for (int i = 0; i < (words.size() - 2); i++){
Alternatively, do as #Uli recommends.
Related
My code prints every number between two limits and makes a sum.
How can i print a new line every 10 numbers?
for (int i = nedreGrense; i <= øvreGrense; i++) {
sum = sum + i;
}
for (int tallStreng = nedreGrense; tallStreng < øvreGrense; tallStreng++){
System.out.print(tallStreng+"+");
}
System.out.print(øvreGrense+"="+sum);
Since your limits can be any integer, so if nedreGrense can be any integer,
then after nedreGrense increases by 10, its ones will not change only its tenth will change, to check we have to use % operator which gives ones of the number.
So use the condition if( (tallStreng != nedreGrense) && ((tallStreng - nedreGrense )%10 == 0))
for (int i = nedreGrense; i <= øvreGrense; i++)
sum = sum + i;
for (int tallStreng = nedreGrense; tallStreng < øvreGrense; tallStreng++){
if( (tallStreng != nedreGrense) && ((tallStreng - nedreGrense)%10 == 0))
System.out.println();
System.out.print(tallStreng+"+");
}
System.out.print(øvreGrense+"="+sum);
I would like someone to assess this code. It's an insertion sort made in Eclipse. I get this error where java.lang.ArrayIndexOutOfBoundsException: -1 whenever I add "=" symbol in the while loop k>=0. If I do remove it, it would run properly but wouldn't give me the expected output of sorting it in ascending order.
For example, if I have values Initial [21, 34, 3, 19, 13] the output would be -> [21, 3, 13, 19, 34].
Logically, having the expression while((temp < n.get(k)) && (k>=0)) is correct arranging it manually, but why does it leave me with that kind of error/exception out of bounds?
int j, k=0, temp, count = 0;
List<Integer> n = set.getvalues();
for(j = 1; j < n.size(); j++)
{
temp = n.get(j);
k = j-1;
while((temp < n.get(k)) && (k >= 0))
{
n.set(k+1, n.get(k));
k = k-1;
count++;
}
n.set(k+1, temp);
count++;
}
You need to check that k >= 0 before attempting to call n.get(k). Attempting to get an element with a negative index doesn't make sense, therefore an ArrayIndexOutOFBoundsException is thrown.
Reorder
while((temp < n.get(k)) && (k >= 0))
to
while((k >= 0) && (temp < n.get(k)))
For logical and (&&) operations, the expression is evaluated from left to right and will stop evaluation as soon as a false is encountered. This is also known as short-circuiting.
Therefore if k >= 0 evaluates to false, n.get(k) will not get called and no exception will be thrown.
As requested the explanation:
while((temp < n.get(k)) && (k >= 0))
{
n.set(k+1, n.get(k));
k = k-1;
count++;
}
The term (k >= 0) in while((temp < n.get(k)) && (k >= 0)) will be checked after (temp < n.get(k)), as conditional statements are left-to-right resolved. So it does check after already accessing with k = -1, which throws that exception, thats why twisting the statements fixes it.
I ve got an issue with the ArrayIndexOutofBound error when trying to determine whether data is heap following the 2i + 1/2 formula child-parent relationship. Do you know how can I resolve the issue?
for (int i = 0; i < array.length; i++)
{
if ((array[i] < array[2*i + 1]) || (array[i] < array[2*i + 2]))
{
bool = false;
....
}
}
I would suggest checking for the boundaries of the array first, and if it contains enough elements, then you can compare them:
if ((array.length >= 2*i + 2)
&& ((array[i] < array[2*i + 1]) || (array[i] < array[2*i + 2])))
{
bool = false;
....
}
Your for-loop guard allows i == array.length -1. But inside the loop, you look for element 2 * i + 2. That's 2 * array.length. That's way beyond the end of the array, and will always throw an excpetion (except when i == 0).
Change your for-loop guard to i < ((array.length / 2) - 1), so that the maximal value of 2 * i + 2 is array.length - 1. Also, ensure you test this both for arrays of even and odd length; I think the logic will work in only one case.
What is the best way to check if a string index is in bounds? Let's say we are checking a String for index i-1 or i+1 because you cannot say != null.
Example:
for (int i = 0; i < string.length(); i++)
{
if (string.charAt(i+1) == '#' && string.charAt(i - 1) != '1')
{
}
}
Should you just check the length of the string and see if it is within it?
string.charAt(i+1) == '#'
Yes, I think you need to make sure i+1 is not greater than String length.
Example:
if( (i+1) < string.length() && (i-1) >= 0 && (yourcode))
{
}
Why not just check the length of the string?
if(myString.length() - 1 > i)
I've always modified the length (and/or start) of the loop in these cases... these are all good answers - there's no one way to do it, but this is how I would do it:
for (int i = 1; i < string.length() - 1; i++)
{
if (string.charAt(i+1) == '#' && (i != 1 || string.charAt(i - 1) != '1'))
{
}
}
EDIT: After considering all of the situations that this code entails, I wouldn't necessarily do this in this manner, but find a cleaner way to express exactly what I'm trying to accomplish
The simplest way is to change your for loop like this:
for (int i = 1; i < string.length() - 1; i++)
{
if (string.charAt(i+1) == '#' && string.charAt(i - 1) != '1')
{
}
}
Explanation:
string.charAt(i - 1) implies that you might read position i-1 of the string. Since 0 is the lowest valid value you can start the iteration with i = 1
string.charAt(i + 1) implies that you might read position i+1 of the string. Since string.length() - 1 is the highest valid value you need to end your iteration at i == string.length() - 2. Since the for-loop checks the condition before entering the loop using i < string.length() - 1 will be just right.
I would try with this code.
for (int i = 1; string!= null && string.length() >= 3 && i < string.length() - 1; i++)
//it solve the case of String == null and string too short
//it optimize the max number of cycles
{
if (string.charAt(i+1) == '#' && string.charAt(i - 1) != '1')
{
}
}
I have some code that I believe to run in O(n), however when I time it, it seems to run in polynomial time. I'm trying to process ~200,000 records, so I did it in blocks of size MAX_COUNT so I wouldn't run out of heap space. That is, during the processing phase, a few things take place that make the records increase dramatically in size.
I copied in the important parts from my code. I feel like something is going on here that has to do with ArrayLists that I just don't understand.
This might not be the smartest way to go about things, but I don't see why it's taking longer to process each block than the previous. That is, each bock is size 5000 (except the first block), but the 1st block processed takes ~5seconds, and the 20th block processed takes ~25seconds. I would expect them to all take the same amount of time.
// Maximum block size
final int MAX_COUNT = 5000;
// Total number of records in need of processing
int n = records.size();
// the number of blocks to process
int numBlocks = (n / MAX_COUNT) + 1;
if (n % MAX_COUNT == 0) numBlocks--;
// The number of records to process in the block.
int numRecords;
ArrayList<Record> recordBlock = null;
// Iterate backwards through the blocks.
for (int i = numBlocks; i > 0; i--) {
// Make sure we don't process too many records.
if ( (i == 1 && numBlocks = 1 && n % MAX_COUNT != 0) ||
(i == numBlocks && n % MAX_COUNT != 0) )
numRecords = n % MAX_COUNT;
else numRecords = MAX_COUNT;
recordBlock = new ArrayList<Record>();
//EDIT: Fixed loop syntax (typo!)
for (int j = numRecords -1; j >= 0; j--)
recordBlock.add(records.remove(j));
recordBlock = ThreadHelper.processRecords(recordBlock,true,true);
while (recordBlock.size() != 0) {
Record r = recordBlock.remove(recordBlock.size() -1);
// write 'r' to MySQL
}
}
This section
for (int j = numRecords -1; j >= j--)
recordBlock.add(records.remove(j));
will reallocate the backing array behind recordBlock every time the backing array is filled. Rather declare it as
recordBlock = new ArrayList<Record>(numRecords);
Also, the loop syntax is incorrect.
As already mentioned by #mcfinnigan
recordBlock = new ArrayList<Record>(numRecords);
In addition, replace
while (recordBlock.size() != 0) {
Record r = recordBlock.remove(recordBlock.size() -1);
// write 'r' to MySQL
}
by
for (Record r: recordBlock) {// write 'r' to MySQL }
recordBlock.clear();
There is a problem with the for loop adding to the recordBlock.
for (int j = numRecords -1; j >= j--)
recordBlock.add(records.remove(j));
should be
for (int j = numRecords -1; j >= 0; j--)
recordBlock.add(records.remove(j));
If I am not mistaken.
Edit:
Another mistake I found was in your if statement.
if ( (i == 1 && numBlocks = 1 && n % MAX_COUNT != 0) ||
(i == numBlocks && n % MAX_COUNT != 0) )
should be
if ( (i == 1 && numBlocks == 1 && n % MAX_COUNT != 0) ||
(i == numBlocks && n % MAX_COUNT != 0) )
Might I suggest simplifying it to:
if(i == numBlocks && n % MAX_COUNT != 0)
since the first condition is just a special case when i = 1.
if the real code has if and for statements without braces then what you think the code does may not actually be what it does.