i want to use a for loop in a function but error occur.. why??

public double weight( double d)
{
for(int i=0;i<5;i++)
{ d /= 24;}

return d;
}

Dani AI

Generated

A quick diagnostic tied to the existing posts: reported a compile error inside a loop and flagged a type concern. The loop construct itself is rarely the root cause. More often the compiler points to a syntax problem introduced by stray characters or misplaced code (for example, HTML emphasis tags injected by a forum editor), unbalanced braces, a missing semicolon, or a method placed outside any class. If the intent was to iterate over multiple values, a primitive value will not behave like an array or list — that distinction is what was referring to.

Practical checks that resolve this class of error most of the time:

  • Put the source in a plain-text .java file and compile with the Java compiler to get the exact file:line:column error location.
  • Scan the source for angle-bracket markup or invisible characters that came from a rich-text editor. Those will break Java syntax.
  • Confirm braces, parentheses, and semicolons are balanced and present, and that the method lives inside a class.
  • If multiple elements are intended, use an array or collection type; if a single numeric variable is intended, a compound assignment is legal in Java.
  • Use an IDE (Eclipse, IntelliJ IDEA, NetBeans) to spot syntax problems visually.

Reference material: the Java tutorials on operators and on arrays explain compound assignment and array usage in detail (Java Operators, Java Arrays). Removing any stray markup or correcting type/placement typically clears the error.

Recommended Answers

All 4 Replies

'd' is a double, not an array.

error in for loop.. this one { d /= 24;}
so how do i solve it??

'd' is not an array.

Just study some basic java on how to declare variables and arrays.

ok2..thanks..:)

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.