For_Loop class is not working as expected on eclipse - java

My For loop is not working and my print statement (i) is showing error in eclipse
Following is the code.
public class For_Loop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++);
System.out.println(i);
}
}

Remove the ; at end of for
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++){
System.out.println(i);
}
}
}

Please remove the semicolon from for loop.
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++)
System.out.println(i);
}
}

Related

Java Integer class code not compiling

I am new to Java.
I am Learning Wrapper class now from Online Resources
The following code does not compile but according to the online material this is giving results
class Integ
{
public static void main(String[] args)
{
Integer I=new Integer.valueOf("1111",2);
System.out.println(I);
}
}
Can you please correct me where i am going wrong.
class Integ
{
public static void main(String[] args)
{
Integer i = Integer.valueOf("1111", 2);
System.out.println(i);
}
}
dont use the new operator, just do Integer.valueOf("1111",2);
public static void main(String[] args)
{
Integer myI = new Integer.valueOf("1111",2);
// ^^^
System.out.println(I);
}
do instead:
public static void main(String[] args)
{
Integer myI = Integer.valueOf("1111",2);
// ^^^
System.out.println(I);
}
You're not supposed to use new. Just remove it:
Integer I = Integer.valueOf("1111",2);

I wrote a method to count positive numbers, but how to call that in main method

public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}

Code exits without printing expected result

I cannot figure this out for the life of me. I have a very simple piece of code that keeps terminating on the console window.
public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
}
}
public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
System.out.println(a);
// Wait until user hits key before quitting
System.in.read();
}
}

Java - assert does not seem to be executed

I do not understand why the output of this code is -10:
public class Prova {
public void sip(int i){
assert i>=0 : err();
System.out.println(i);
}
public int err(){
...
}
public static void main(String[] args) {
Prova t = new Prova();
t.sip(-10);
}
}
I thought assert would launch err();

Using Objects in a similar way to Variables in Java

Can i use the following code? It's not throwing any error at the Object but at obj.i. Is this a legal way of using an object? Also, how many ways can i create an object other than using the normal syntax obj s = new obj();
public class Test {
static int i;
static Test obj;
obj.i = 10; //am getting a compilation error here "Syntax error on token "i", VariableDeclaratorId expected after this token"
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
You need to place a static block around the obj.i assignment statement for this to work:
public class Test {
static int i;
static Test obj;
static { obj.i = 10; }
public static void main(String[] args) {
System.out.println(i+" "+ obj);
}
}
This is not, you didn't initialize. Furthermore you might not want to use static.
public static void main(String [] args) {
int i = 10;
Test obj = new Test();
obj.setI(i);
System.out.println("my objects I = "+ obj.getI());
}
now in your Test object
public class Test {
private int i;
public void setI(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}

Categories

Resources