can someone help me and tell me what is wrong, I can't figure it out. This is what it is exactly saying

Lab424.java:14: cannot find symbol
symbol : method printf(java.lang.String, float)
location: class Lab424
printf(" sum %f\n", sum);

it is pointing to the "p"

import java.util.Scanner;

public class Lab424
{
public static void main(String[] args) {

int i; float sum;
i=3;
sum=0;
while(i<=99)
{
sum=sum + ( (i-2)/i);
i=i+2;
}
printf(" sum %f\n", sum);
}
}

There is no 'printf()' in Java. This is C++ syntax.

Use

System.out.println("Sum is " + sum);

or

System.out.println(String.format("Sum is %f", sum));

is you want to format.

Also, be careful using (i-2)/i. i is an Integer, and (i-2)/i will always result in 0 if you don't explicitly tell Java to use it as a float (like this: ((float)i-2)/(float)i).

Changed code:

public static void main(String[] args) {
		float sum=0;
		for (int i=3; i<=99; i+=2) {
			sum += (((float)i - 2) / (float)i);
		}
		System.out.println (String.format (" sum %f\n", sum));
	}

Good luck.

Hmmmm....

(And with respect to the next error message, remember 'System.out.' ;) )

:-/

commented: ;) +15

There is no 'printf()' in Java.

Sorry, but yes there is. It was added in Java 1.5 This is from the API doc for PrintStream:

public PrintStream printf(String format, Object... args)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

commented: Useful information. +7

Wow, that's new for me. I have never needed it before. In Eclipse, there was also no import warning, so where does that method come from?

It's just a perfectly normal method in the java.io.PrintStream class, just like print and println. It's just a bit newer, that's all. If you create a PrintStream explicitly you'll need to import it, but it's normally used via System.out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.