Here is my code :
private String SerialNo;
private String FirmVersion;
public String GetSerial(int[] Data){
System.out.println("GetSerial Debug : Data => "+Data);
for (int i = 2;i==13;i++){
System.out.println("In the FOR => ok ");
if (i != 9){
SerialNo = SerialNo + Data[i];
}
if (i == 9){
SerialNo = SerialNo + ".";
}
}
System.out.println("SerialNo => "+ SerialNo);
return SerialNo;
}
My problem : I can't "enter" in the FOR
So my sysout of "In the FOR => ok", never shows and all the "actions" aren't done.
What am I doing wrong ?
ps : I'm sure that I'm compiling the right file.
The loop condition is never satisfied; i = 2 in the begin, the first check would fail, so all the loop would fail. Maybe it should be changed for:
for (int i = 2; i <= 13; ++i)
Examine your for statement:
for (int i = 2; i==13; i++)
This actually means the following:
assign 2 to i
Check whether i equal to 13. If yes, continue loop, exit otherwise.
Since i is not 13 in the first iteration of loop you never enter it. I believe that you wanted to write
for (int i = 2; i <= 13; i++)
In this case you will iterate from 2 to 13 inclusively. The condition of for loop means "do I have to remain iterating?" and not "do I have to escape?"
Change for (int i = 2; i == 13; i++) to for (int i = 2; i <= 13; i++).
The second argument is the loop condition which has to be true to run the loop.
Your condition became false at first iteration so control never goes to loop body.
for loop syntax:
for(initialization; condition; increment/ decrement){
//your code
}
So here you will have to use some appropriate condition to enter into the loop.
So for example :
for (int i = 0; i <= 13; i++) // for 0 to 13 increment
or
for (int i = 10; i >= 0; i--) // for 10 to 0 decrement
for (int i = 2;i==13;i++){}
It enters but failed at first condition check and for-loop exits.
It should be -
for (int i = 2;i<=13;i++)
You have initialized i=2
for (int i = 2;i==13;i++)
And condition is i==13 which will become false ultimately flow never enter into for loop
try to change the code like this
for (int i = 2;i<=13;i++)
Statement is not good should be like the one below:
for (int i = 2; i<13; i++) or for (int i = 2; i<=13; i++)
See compared simple while loop in the case of your for loop.
Think about int i =2; value set and i == 13 condition
Do you think it will work?
for (int i = 2;i==13;i++){
//do something
}
Same to below *while loop* explanation
int i = 2;
while (i == 13) {
//do something
i++;
}
I am sure it will work
for (int i = 2;i < 13;i++){
//do something
}
Same to below **while loop**
int i = 2;
while (i < 13) {
//do something
i++;
}
The flow of the for loop is: init statement-> condition check-> goes inside loop or outside depending on the condition outcome.
Here, since you've said i=2 ,then i==13 is false; it'll never go inside the loop.
You could use the ?: operator in the for loop and then modify your if statements a bit I guess..
Related
Let's say we have a for loop
for(int i = 0; i < 10; i++) {
...
}
What format is the i++ in?
I have a String increment = "i++" which tells me how much to increment i by.
But when I just directly try and put the increment string as an incrementor it doesn't work. I tried the below but it doesn't work.
for(int i = 0; i < 10; increment) {
...
}
How can I make this work?
The increment need to be an direct attribution,
you can't use a String to define this, is impossible
One solution to change dynamically how much will be the increment, it's using a variable
int j=1;
for(int i=0;i<10;i+=j)
the += operator it's used for the increment, for example:
int x = 0;
x += 1; //x is 1
x += 3; //x is 4
x += 2; //x is 6
x++; //x is 7
But in the most cases you don't need to use this in a for loop, what you need to do?
Hugs
You could parse the increment string to an int then use that as your increment in your loop:
String increment = "i+2"; // Will also work for "i++" or "i+n" (n = a number)
String num = increment.replaceAll("i\\+*", "");
int incValue = 1;
if(!num.isEmpty())
incValue = Integer.parseInt(num);
for(int i = 0; i < 10 ; i+=incValue)
{
System.out.println(i);
}
So for this, you cannot use a string as a expression.
There will be quite a few ways to map a String to expression.
One of the ways is you can use switch case:
String increment = "i++";
int i = 0;
while(i < 10)
{
//your logic here
switch (increment){
case "i++":
i++;
break;
case "i=i+2":
i = i + 2;
break;
}
}
You can also use Regex or parse the String to convert to int
You can simply get increment count in a string if you want to do increment dynamically, instead of whole increment statement. Here is an example:
String inc = "2";
String max = "10";
for(int i = 0; i < Integer.valueOf(max); i=i+Integer.valueOf(inc)) {
System.out.println(i);
}
i++ is just code : it's not a string as you thought.
Here's an analogy to demonstrate your assumption:
String someString =
"public static void doStuff()
{
System.out.println("this is working");
}";
Now if you refer to the String someString in a separate method, do you think that the doStuff() method will be called? Of course not, because String is just text that the interpreter doesn't go through. The interpreter can only go through executable code, which includes the i++ in question.
I want to loop infinitely using a for loop if a number equals 0, and loop until that number number if the number is greater than 0. Here's the code to help visual what I'm getting at.
for (int i = 0; i < this.getNumRounds(); i++) {
// 30 some lines of code
}
or
for ( ; ; ) {
// 30 some lines of code
}
if getNumRounds() is greater than 0, do the first loop, if it equals 0, do the second. I would prefer to do this without copying and pasting my 30 some lines of code twice and using an if statement seeing as the code is redundant, though I could use a function to take out that redundancy, but I'm looking to see if there's another option.
Use the powerful ternary operator:
for (int i = 0; this.getNumRounds() == 0 ? true : i < this.getNumRounds(); i++) {
// 30 some lines of code
}
As noted in the comments by yshavit, there is a shorter, cleaner way of expressing this:
for (int i = 0; this.getNumRounds() == 0 || i < this.getNumRounds(); i++) {
// 30 some lines of code
}
Have you thought about using a while loop instead?
int i = 0;
while(i < this.getNumRounds() || this.getNumRounds() == 0) {
//some 30 lines code
i++
}
So you want something like this:
int num = //whatever your number equals
if (num == 0) {
boolean running = true;
while (running) {
doLoop();
}
} else {
for (int i = 0; i < num; i++) {
doLoop();
}
}
private void doLoop() {
//some 30 lines of code
}
This code puts the contents of the loop in a separate method and checks if the number is equal to 0. If it is, the program runs the doLoop() method forever. Otherwise, it runs until i equals the number.
While it would be better to just create a method and use an if-statement you could add an if statement inside the for-loop to decrease i every iteration. It would look like:
for (int i = 0; i <= this.getNumRounds(); i++) {
if(this.getNumRounds() == 0){
i--;
}
// 30 some lines of code
}
Notice I changed i < this.getNumRounds() to i <= this.getNumRounds. This way if the number of rounds is zero then the loop will be called.
You could do the following.
for (int i = 0; i < this.getNumRounds() || i == 0; ++i) {
do {
// 30 lines of code
} while (this.getNumRounds() == 0);
}
If getNumRounds is non-trivial to compute, consider pulling it out of the loop and calling it only once.
I'm putting together a code for a hailstone sequence I found in an online tutorial, but in doing so I ran into an unreachable statement error. I don't know if my code is correct and I don't want advice in correcting it if I'm wrong(regarding the hailstone sequence, I want to do that myself..:) ). I just want help in resolving the "unreachable statement" error at line 19.
class HailstoneSequence {
public static void main(String[] args) {
int[][] a = new int[10][];
a[0][0] = 125;
int number = 125;
for (int i = 0;; i++) {
for (int j = 1; j < 10; j++) {
if (number % 2 == 0) {
a[i][j] = number / 2;
number = number / 2;
} else {
a[i][j] = (number * 3) + 1;
number = (number * 3) + 1;
}
}
}
for (int i = 0;; i++) {
for (int j = 0; j < 10; j++) {
System.out.println(a[i][j]);
}
}
}
}
This is an infinite loop:
for(int i=0;;i++){
Whatever comes after it never get executed (i.e. is unreachable).
In your first for loop:
for(int i=0;;i++){
....
}
You do not define the ending condition. e.g.
for(int i=0; i<10; i++){
....
}
Therefore the loop never exits.
Your first infinite loop of for(int i=0;;i++) stops any other code from being reached.
There is an infinite loop # line 7
You forgot to set an exit condition
for(int i=0;here;i++){
This might create unexpected behaviour.
Your first for statement (in the 6'th line) is an infinite loop therefore it stops further code to be reached.
for(int i=0;;i++)
You have problem at line number 6 in your first for loop.
for(int i=0;;i++) {
Here since your don't have any exit conditions, code is going in the infinite loop and the loop never exits. Whatever outside the scope of this for loop will be unreachable since your first loop is never existing.
Consider adding the exit condition (such as break or return etc.) inside your for loop to prevent this behavior.
I am in a beginner Java class and for a project I need to count how many times a condition returns TRUE(correctGuess) or FALSE(incorrectGuess) with a loop inside of a loop. The problem that I'm having is that the variables being incremented within the inner loop do not hold their incremented value as the loop reiterates. Therefore, the outer while-loop's condition is never false. I'm really new to programming and I can't figure out the solution. Thank you in advance for your time with this silly question and if there are any questions I would be happy do a better explanation. The code looks like this:
int incorrectGuess = 0;
int correctGuess = 0;
while(incorrectGuess < 6 && correctGuess < WORD_LENGTH) {
//Gets the users first guess
System.out.print("Please guess a letter [A-Z]: ");
letterGuessed = keyboard.nextLine();
for (int i = 0; i < WORD_LENGTH; i++){
char value = wordLetterArray[i];
String letterArray_value = String.valueOf(value);
if(letterGuessed.equals(letterArray_value)){
++correctGuess;
}
else
System.out.println("Bad comparison!");
if(i == WORD_LENGTH)
++incorrectGuess;
}
}
Looks like you may need to redesign the whole algorithm, but I can tell you what your main issue is with this looping forever:
// Seems legit
while(incorrectGuess < 6 && correctGuess < WORD_LENGTH) {
// Still seems legit
for (int i = 0; i < WORD_LENGTH; i++)
// Well, there's a problem, i will never be equal to word length
//because a condition of the for loop is i < WORD_LENGTH
if(i == WORD_LENGTH)
++incorrectGuess;
Again, I feel you need to redesign your whole algorithm, but if you want it to continue, just pull the incorrectGuess increment line out of the for loop. This will give you the intended result:
for (int i = 0; i < WORD_LENGTH; i++){
char value = wordLetterArray[i];
String letterArray_value = String.valueOf(value);
if(letterGuessed.equals(letterArray_value)){
++correctGuess;
}
else {
System.out.println("Bad comparison!");
}
}
incorrectGuess++;
I have come across a somewhat annoying problem during a project. I created this sample class to describe the issue which I am having.
public class Test {
public static void Testing(){
for (int i = 0; i >= 5; i++) {
System.out.println(i);
}
System.out.println("hello world.");
}
public static void main(String[] args) {
Testing();
}
}
My issue is that the only output from this program is simply "hello world."
Could anyone explain the reason why my println statement inside the for loop is being completely ignored? I have searched on Google but it is hard to describe in a search.
Thanks a lot!
The for loop should be
for (int i = 0; i <= 5; i++)
Hai Buddy. the problem is logical.look at the for loop closely for (int i = 0; i >= 5; i++)
The for loop should be
for (int i = 0; i <= 5; i++)
I think the problem is that you loop never executes since your condition is that I is at least 5, but you start it at zero. Try changing it to be less than or equal to five and see if that fixes it.
change the for loop
for(int i = 0; i <= 5; i++)
read again:
for (int i = 0; i >= 5; i++)
i defaults to zero, and the for iterates while i is more or equal to 5.
Because your condition (i >= 5) never is true, since you set i to 0. The condition should be i <= 5.
for (int i = 0; i <= 5; i++) //You have put > sign it should be < sign
{
System.out.println(i);
}
The reason is that your for loop is never executed. On first step i = 0 i>=5 = false so the body of the for is never executed
When main method invoke you method it first initialize the value of i with 0 then its go for the condition i>=5, Which looks like 0 >= 5 which always be 'false'.So you inner print statement never be execute.
The for loop never execute because the at the beginning i is checked to see if it equal or greater than 5 (which it is not, i=0)
for (int i = 0; i >= 5; i++)
then the loop terminates and the next statement is executed.