Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
for (int i = 0; i < totalArray.size(); i++){
studentNumber.add(Long.parseLong(totalArray.get(i * 3)))
lastName.add(totalArray.get((i * 3) + 1));
firstName.add(totalArray.get((i * 3) + 2));
I'm not sure what is going on here. I've getting an index out of bounds error on this code. totalArray.size() is 42 however I'm getting the error at index 42 with the second line (parseLong).
As cricket_007 points out, on the 14th iteration of your for statement, i is 14 and your second line attempts to access element 42 of your index (where the last element of your array is accessed as element 41 because it's 0 indexed).
To stop the for statement at the proper place, merely do the inverse operations which you do on your last line of code.
i < (totalArray.size()-2)/3
(which was pointed out by Benjamin Lowry).
You're multiplying your i by 3 in your studentNumber.add(Long.parseLong(totalArray.get(i * 3))) line, so when i >= 14, it goes out of bounds.
Try setting it to studentNumber.add(Long.parseLong(totalArray.get(i))) and run it. You probably wanted to place your * 3 outside the first parenthesis, but I can't deduct your logic.
When i is 14, you are getting the index 42 with i * 3. Which is out of bounds for a list of size 42.
Your third line triples the number. This means that if i was 30 (which is possible with your current set up), then when tripled, would equal 90, which is obviously above your max size.
You need to do
for (int i = 0; i < (totalArray.size() - 2) / 3 ; i++)
In order for your code to work. Otherwise all three of your lines will exceed the max at some point.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
int keyCardNum = 10;
for(int x = 0; x<= keyCardNum; x++) {
System.out.println(x);
}
It prints the following:
0
1
2
3
4
5
6
7
8
9
10
Online I read that for loops start from 0. It that true and how can I remove the 0 and start from 1 to 10.
for(int x = 0; x <= keyCardNum; x++)
Defines three properties of the for loop:
int x = 0: one or more loop variables and their initial value. Executed once when the loop starts. Usually, this variable is called i.
x <= keyCardNum: the loop condition. Executed once per iteration. The loop terminates when this condition evaluates false. (the loop never runs if the condition evaluates to false immediately). Usually this is a less-than (<) expression. i=0;i<10;++i will loop 10 times, i=0;i<=10;++i will loop 11 times.
x++: the incrementor. Executed once per iteration. Here you define what happens after each loop iteration. Usually, you increment the loop counter variable, but you could do anything here.
The loop body is executed as many times as the loop condition evaluates to true. If you are interested in the nitty-gritty details, the Java Language Specification has them.
If you want a loop to start at 1, you have to initialize the loop variable with 1. Or, you "normalize" your loop variable when using them in an expression, e.g. x + 1 to produce a value offset by 1 (i.e. 1-11 in your example).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a question: Can some one give me an example of searching the binary table to find the change from 1 to 0? For example, one line have
0 0 0 1 1 0 0 0 0 1 1 1 0 0
and it should give me 2 changes. I want to search only one line.
for(int i=1; i< binarytableline.length; i++){
if (binarytableline[i - 1] == 1 && line[i] == 0) changes++;
}
Remember that the indexes start at zero:
(Java docs)
Essentially, whats happening in the code above is that we are looping through the line. Now, i is the index. Lets start at the beginning of the loop.
i=1
So, binarytableline[1-1] = the first index of the binary table line, or the very first number. Now, we are seeing if it equals 1, and the second index, which is i, equals 0. To check, we do the following:
binarytableline[i - 1] == 1 && line[i] == 0
This would mean that there is a change in binary digits from one digit to the next, and in our example that is the 0th index to the first. Now, we would iterate our variable, changes by 1 by doing changes++. Again, this is in a for loop, meaning we will loop through all the elements like this. The amount of times it has changed will be recorded in int changes.
Let me know if this helped,
Ruchir
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
A sample of the statement to type when using the method is
System.arraycopy(data, i, data, i+1, data.length-i-1);
The way it works according to my book is that the system move all element from i onward one position up. But intuitively I will think that the method will move all the element after i one position down. So there is empty space to put the copied element.
I am confused now as to how to move elements around in the same array. What does the statement really say?
The definition of System.arraycopy is
System.arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
Therefore, this code copies
data[i .. (i + n - i - 1)] = data[i..(n-1)]
to
data[i+1 .. (i+1 + n-i-1)] = data[i+1..n]
(where the range includes the front but excludes the back, and n = data.length)
and thus leaves a "blank" spot at data[i]. Graphically:
[0] [1] [2] [3] ... [i] [i+1] [i+2] ... [n-2] [n-1]
src |-----------------------|
dest |-------------------------|
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a loop that checks the biggest number so far and adds one to it; but for example 1 2 3 4 5 I press button 6 is created, what if the list was 1 2 3 5 6 and I want it to print 4 so I can get a correct number line then proceed to increase it by one?
EDIT: I should mention that my list has a possiblity not being in order so 2 1 3 5
You should check incrementally that the numbers are good:
So say you have an array of int called numbers[]
you could do:
int placehodler = numbers[0] + 1;
for (int i = 1; i < numbers.length(); i++){
if (placeholder != numbers[i]) // checks if the next number is equal to the previous + 1
break; // breaks out of the loop if it isn't
placeholder++; // increment placeholder by 1
}
System.out.println(placeholder); // prints placeholder
So if the placeholder breaks out of the loop early, it is your 1,2,3,5,6 situation and it would have 4 (succeeds on the loop where placeholder = 3, fails when placeholder = 4) in the 1,2,3,4,5 situation, it would succeed on all and end the loop with a value of 6.
First, I would find the count of your current list. If your list is [1 2 3 5] the count is 4.
Then, I would create a new list that is a range of numbers from 1 to the count => [1 2 3 4]
comparing the two lists will let you see if any numbers are missing, if no numbers are missing then you can add 1 to the current count and append that to the end of the list.
sorting the list before doing any of this will take care of the out of order issue mentioned in the other comment.