• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

if , for loop auto populate in eclipse

if loop auto populate in eclipse i thought i have to click space after typing if. But i found i have to press control space. I woder when to type press to see available options when to type control space. also how to format code in eclipse when we have inner if loops as attached. As of now i clicked source and format option of eclipse which did not indent clearly.

Also for loop same control space approach did not show any for(i=0;i<n;i++) kind of regular for loop syntax to select.
please advise
ifLoop.jpg
ifLoop2.jpg
ForLoop.jpg
0
gudii9
Asked:
gudii9
  • 12
  • 7
1 Solution
 
gudii9Author Commented:
public class Test56 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	//	int a = 100;
	/*	if (a < 200) {
			System.out.println(" true part");
		}
		if (a < 150) {
			System.out.println("level1");

		}
		if (a < 125) {
			System.out.println("level2");
		}

		else {
			System.out.println("faslse");
		}
	}*/
	
	for (int i = 0; i < args.length; i++) {
		System.out.println(i);
		System.out.println(",");
	}

}
}

Open in new window


i wonder above code is not printing anything. There is no error either. please advise
0
 
chaauCommented:
Do you pass any arguments to the main()? I mean, when you start your application do you start it with any arguments?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
when to type press to see available options when to type control space.
It is only ever control+space to see content assist. I don't know what you are thinking of with just space, if you can describe where you have seen this that would help.

As of now i clicked source and format option of eclipse which did not indent clearly.
You could try "selecting all" first before trying the format command.

Also for loop same control space approach did not show any for(i=0;i<n;i++) kind of regular for loop syntax to select.
From the screenshot it looks like you typed for OUTSIDE the main method. Code is not allowed outside of a method and so Eclipse didn't suggest to auto-complete code because it would be in the wrong location. Most of these control+space content assist things are context sensitive, you have to type it in the right place. So try typing "for" inside the main method body and then press control+space and you should get the content assist.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gudii9Author Commented:
From the screenshot it looks like you typed for OUTSIDE the main method. Code is not allowed outside of a method and so Eclipse didn't suggest to auto-complete code because it would be in the wrong location. Most of these control+space content assist things are context sensitive, you have to type it in the right place. So try typing "for" inside the main method body and then press control+space and you should get the content assist.

when i commented above if block cod i see content assist for for loop which is bit weird. I still do not see output for for loop printing numbers till 20. Not sure why . please advise

You could try "selecting all" first before trying the format command.

i tried that but still inner if is not indenting deep enough to differentiate outer if with iner if
0
 
gudii9Author Commented:
public class Test56 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 100;
		if (a < 200) {
			System.out.println(" true part");
		}
		if (a < 150) {
			System.out.println("level1");

		}
		if (a < 125) {
			System.out.println("level2");
		}

		else {
			System.out.println("faslse");
		}
	}
	
	for (int i = 0; i < args.length; i++) {
		System.out.println("i values is"+i);
		System.out.println(",");
	}

}
}

Open in new window


when i keep both if and for loop getting compilation error saying

Syntax error on token "}", { expected after this token

i wonder what that means. Cannot i have if and for together. Please advise
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
when i commented above if block cod i see content assist for for loop which is bit weird. I still do not see output for for loop printing numbers till 20. Not sure why . please advise
Why would you be expecting 20? What that loop is doing is executing once through the loop for every command line argument that you pass to your program. And if you are not passing any arguments you will get no output. If you just want to see the numbers up to 20, try this...

for (int i = 1; i <= 20; i++) {
    System.out.println("Number: " + i);
}

Open in new window



i tried that but still inner if is not indenting deep enough to differentiate outer if with iner if
It would, it's just that you don't have any inner loops to be further indented! Each of those if statements is at the same level, there is no inner/outer with what you have posted.



i wonder what that means. Cannot i have if and for together. Please advise
Of course you can have them both together. The problem is that you have an extra    }    at the very end of your code. If you remove that, then you will find that you still have the same problem where the for loop is NOT inside the main method. Your main method starts on line 6 and finished on line 23. You can see this easily due to the (correct) indentation that you have. Your for loop comes after line 23 and so is not within any method, and you can't have code that is NOT part of a method.
0
 
gudii9Author Commented:
i see your points. I fixed those } issues as below
public class Test56 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 139;
		if (a < 200) {
			System.out.println(" true part");
		}
		if (a < 150) {
			System.out.println("level1");

		}
		if (a < 140) {
			System.out.println("level1.1");

		}
		if (a < 125) {
			System.out.println("level2");
		}

		else {
			System.out.println("faslse");
		}

		for (int i = 0; i < 20; i++) {
			System.out.println("i values is" + i);
			System.out.println(",");
		}

	}
}

Open in new window


I still not sure how can i modify my code to see and indent inner if loop. please advise
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I still not sure how can i modify my code to see and indent inner if loop. please advise
Again, there is NO inner if statement in your code so the indenting is correct as it is.
0
 
gudii9Author Commented:
public class qq111 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num = 9;
		if (num > 0)
			if (num < 10)
				if (num % 2 == 0)
					System.out.println("num is between 0 and 10 and even");
				else {
					System.out.println("num is between 0 and 10 and odd");
				}

	}

}




what is difference between above withouth {} in outer if and with {} in outer else as below.
In above case how it is working without {} and how to put outer else in this case.






public class qq111 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num = 9;
		if (num > 0)
			if (num < 10)
				if (num % 2 == 0)
					System.out.println("num is between 0 and 10 and even");
				else {
					System.out.println("num is between 0 and 10 and odd");
				}

	}

}

Open in new window


/now i see it is indenting properly. please advise
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
The two pieces of code appear exactly the same to me but I guess you meant one of them to look like the below...

public class qq111 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num = 9;
		if (num > 0) {
			if (num < 10) {
				if (num % 2 == 0) {
					System.out.println("num is between 0 and 10 and even");
				} else {
					System.out.println("num is between 0 and 10 and odd");
				}
			}
		}

	}

}

Open in new window


Well, there is NO difference in this case. In this case you can do it without the braces because there is only one statement inside the if statement. However, I would suggest that it is good practise to always use   {   and   }   in your code. You can easily get into trouble if you leave them out.

And yes, now you have inner if statements and so Eclipse is indenting the code as you were expecting.
0
 
gudii9Author Commented:
Well, there is NO difference in this case. In this case you can do it without the braces because there is only one statement inside the if statement.

i thought there are more than one statement as bolded below right within outer if loop

if (num < 10) {
                        if (num % 2 == 0) {
                              System.out.println("num is between 0 and 10 and even");
                        } else {
                              System.out.println("num is between 0 and 10 and odd");
                        }

                  }//else{ System.out.println("num is more than 10 ");}??

How to put outer else (else{ System.out.println("num is more than 10 ");}??) as above if i do not have those {} in outer if loop? please advise
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
i thought there are more than one statement as bolded below right within outer if loop
Maybe I should have said statement block. The bolded part is ONE statement block, even though it may contain may individual statements.

How to put outer else (else{ System.out.println("num is more than 10 ");}??) as above if i do not have those {} in outer if loop?
You can do this if only for your learning about the subject, like below...

if (num < 10) 
                        if (num % 2 == 0) {
                              System.out.println("num is between 0 and 10 and even");
                        } else {
                              System.out.println("num is between 0 and 10 and odd");
                        }
                  else{ System.out.println("num is more than 10 ");}

Open in new window


But as I explained before, if I were you I would not concentrate on this at all. Just use { and } everywhere and you don't have to even think about these things!
0
 
gudii9Author Commented:
Maybe I should have said statement block. The bolded part is ONE statement block, even though it may contain may individual statements.

i see your point now.  bolded one is outer if statement block right more specifically(if (num < 10) statement block). please advise
0
 
gudii9Author Commented:
if (num < 10) 
                   [b]     if (num % 2 == 0) {
                              System.out.println("num is between 0 and 10 and even");
                        } else {
                              System.out.println("num is between 0 and 10 and odd");
                        }[/b]
                  //else{ System.out.println("num is more than 10 ");}??

Open in new window


can i write outer if block as above without openeing and closing { }
if Yes how can put  else loop to it.

I understand putting { } is best thing but just wondering if we can write if block with multiple statements without { }. Does compiler know where block starting and ending withouth

{}

please advise
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
can i write outer if block as above without openeing and closing { }
Yes because the inner if-else counts as only one statement in terms of what the outer if statement sees.

if Yes how can put  else loop to it.
It should work exactly as you have it in the line that is commented out. Did you try and see what the compiler says? Because that last println statement is also just a single statement, you could also leave the { and } out of the else too.

just wondering if we can write if block with multiple statements without { }
NO, you can't. I thought we've been over this, but this is the whole point to this discussion: you can only leave the { and } out, in either the if part or the else part, if they contain only 1 statement.
0
 
gudii9Author Commented:
: you can only leave the { and } out, in either the if part or the else part, if they contain only 1 statement.

else also can have inner if-else?
inner if-else counts as only one statement in terms of what the outer if statement sees.
0
 
gudii9Author Commented:
public class TEst111 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int num=11;
		// TODO Auto-generated method stub
		if (num < 10) 
            if (num % 2 == 0) {
                  System.out.println("num is between 0 and 10 and even");
                  System.out.println("test inner if else without brakets ");
            } else {
                  System.out.println("num is between 0 and 10 and odd");
            }
      else 
    	  if (num < 13) 
              if (num % 2 == 0) {
                    System.out.println("num is between 10 and 13 and even");
              } else {
                    System.out.println("num is between 10 and 13 and odd");
              }

	
}
}

Open in new window


it all worked fine as above
num is between 10 and 13 and odd
0
 
gudii9Author Commented:
looks like inner if i cannot have without brackets having two system out statements as below
public class TEst111 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int num=11;
		// TODO Auto-generated method stub
		if (num < 10) 
            if (num % 2 == 0) 
                  System.out.println("num is between 0 and 10 and even");
                  System.out.println("test inner if else without brakets ");
           else {
                  System.out.println("num is between 0 and 10 and odd");
            }
      else 
    	  if (num < 13) 
              if (num % 2 == 0) {
                    System.out.println("num is between 10 and 13 and even");
              } else {
                    System.out.println("num is between 10 and 13 and odd");
              }

	
}
}

Open in new window


please advise
0
 
gudii9Author Commented:
An if statement with its optional else statement is being treated by the compiler as one statement.  so uter loop it does not matter if { } are there or not.

where as inner if loop if i have two lines of sysouts they are consider as separate two statements which makes comiler to complain saying need {}
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I think you've got it! ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 12
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now