hey guys:
i put a question 2 days ago after having a problem with this equation and u guys helped me alot and solved it but i want to program it in java everything went well but gave me 2 errors here is the code and all information the code suppose to find the root of this equation "X cube minus 3X plus 1" on [0,1] after 5 iterations "Bisection Method"

import java.lang.*;
    import java.util.*;
public class abc
{
  public static void main(String[] args);
  {
     
    int max=5;
    double EPS=0.01;                
     double f(double x)
     {
            return ((x*x*x)-(3*x)+1);
    }
            double A,B,root;
           
            System.out.println("enter 2");
            System.out.println("A :");
            A=console.nextInt();
            System.out.println("B :");
            B=console.nextInt();
           
            root = bisect(a,b);
           
            System.out.println("root is "+root);
    } 
    
    
    double bisect(double a,double b){
            int n;
            double c;
           
            for(n=1;n<max;n++){
                    c=(a+b)/2.0;
                    if(fabs(f(c))<EPS)        break;         
                    if(f(a)*f(c)<0.0)        b=c;
                    else a=c;
            }
            return c;
}
}

errors info:
2 errors found:
File:....\mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected
File: ....\mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected

and thank you so much

Recommended Answers

All 23 Replies

you're trying to create methods within your main method . don't.
also, you have a return statement in your main method, while the main method is void
also, you're using A and B, but you're never declaring them.

you're trying to create methods within your main method . don't.
also, you have a return statement in your main method, while the main method is void
also, you're using A and B, but you're never declaring them.

all fixed and code edited still has two errors thnx anyway ;)

what errors? I just read over your code here, it's not like I compiled and/or ran it here myself.
show your code how it is now and the errors you're getting. also: do you get them by compiling, or by running the code?

what errors? I just read over your code here, it's not like I compiled and/or ran it here myself.
show your code how it is now and the errors you're getting. also: do you get them by compiling, or by running the code?

nothing has changed my friend same 2 errors found:
File:....\mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected
File: ....\mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected
the code after editing is in the original post
i get the errors by compiling

what do you mean 'nothing has changed'? I told you what mistakes you made in your code.
unless you actually correct those mistakes, it's quite normal you keep getting these errors.

what do you mean 'nothing has changed'? I told you what mistakes you made in your code.
unless you actually correct those mistakes, it's quite normal you keep getting these errors.

it's seems like you don't understand me "you helped me " and i thanked you then i fixed the errors in the program and still have 2 errors and i fixed the same errors you told me about on the site in my orginal post So i keep seeing the same 2 errors that i was looking for an explanation for them when i compile :)

made a mistake there: you did declare A and B, but they're not int's, they're doubles.

oh yeah, replace the line

public static void main(String[] args);

by

public static void main(String[] args)

you'll also need to have a 'console' variable before trying to use it

I just tried to compile your code here, to see a bit of the results.
might be a good idea to start from scratch:

you're using variables you haven't declared, you're not taking into account Java is case sensitive, you have a return statement in your main method, you're trying to write methods within the main method, you're not familiar with when to declare a method as static ...

either start over, or compile, and each time solve the problem you're compîler is giving you.

it told you exactly what line your problem could be found, so it shouldn't have been to hard to detect. read the error messages you get, and fix the problem it describes, on the line where it says the problem is, and you'll be just fine.

I just tried to compile your code here, to see a bit of the results.
might be a good idea to start from scratch:

you're using variables you haven't declared, you're not taking into account Java is case sensitive, you have a return statement in your main method, you're trying to write methods within the main method, you're not familiar with when to declare a method as static ...

either start over, or compile, and each time solve the problem you're compîler is giving you.

it told you exactly what line your problem could be found, so it shouldn't have been to hard to detect. read the error messages you get, and fix the problem it describes, on the line where it says the problem is, and you'll be just fine.

here is my code after editing all mistakes made but i still have errors

import java.lang.*;
    import java.util.*;
public class abc
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args);
  {
     
    int max=5;
    double EPS=0.01;                
     double f(double x)
     {
            return ((x*x*x)-(3*x)+1);
    }
            double a,a,root;
           
            System.out.println("enter 2");
            System.out.println("A :");
            a=console.nextDouble();
            System.out.println("B :");
            b=console.nextDouble();
           
            root = bisect(a,b);
           
            System.out.println("root is "+root);
    } 
    
    
    double bisect(double a,double b){
            int n;
            double c;
           
            for(n=1;n<max;n++){
                    c=(a+b)/2.0;
                    if(fabs(f(c))<EPS)        break;         
                    if(f(a)*f(c)<0.0)        b=c;
                    else a=c;
            }
            return c;
}
}

2 errors found:
File: C:\...\mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected
File: C:\...mp\mypros\mypros\abc.java [line: 10]
Error: ';' expected

stulkuske has already told you
1. to remove the ; on public static void main(String[] args);
2. you can't define a method inside another method definition.
If you don't listen you won't learn.

stulkuske has already told you
1. to remove the ; on public static void main(String[] args);
2. you can't define a method inside another method definition.
If you don't listen you won't learn.

oh sorry ok the first one is fixed but the second one i don't understand how ??
can u implement it on the code or give me an example thanks

you have something like:

public static void main(String[] args) {
   ...
   double f(double x) {
      ...
   }
}

that's one method definition inside another, which is illegal. You must finish the first method before you start the second, like this:

public static void main(String[] args) {
   ...
}
double f(double x) {
   ... 
}

and since you're using that method from within the main method: take a look at the principles of when declaring a method or variable static and when not

you have something like:

public static void main(String[] args) {
   ...
   double f(double x) {
      ...
   }
}

that's one method definition inside another, which is illegal. You must finish the first method before you start the second, like this:

public static void main(String[] args) {
   ...
}
double f(double x) {
   ... 
}

after doing everything here is the code;

import java.lang.*;
    import java.util.*;
    import java.io.*;
public class abc
{
  static Scanner console = new Scanner(System.in);
  public static void main(String[] args)
  {
    int max=5;
    double EPS=0.01;                
            double a,b,root;
           
            System.out.println("enter 2");
            System.out.println("A :");
            a=console.nextDouble();
            System.out.println("B :");
           b=console.nextDouble();
           
            root = bisect(a,b);
           
            System.out.println("root is "+root);
    } 
    
    
     public static double bisect(double a,double b){
            int n;
            double c;
           
            for(n=1;n<max;n++){
                    c=(a+b)/2.0;
                    if(fabs(f(c))<EPS)        break;         
                    if(f(a)*f(c)<0.0)        b=c;
                    else a=c;
            }
            return c;
}
     double f(double x)
     {
            return ((x*x*x)-(3*x)+1);
    }
}

and here is the errors
6 errors found:
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 29]
Error: cannot find symbol
symbol: variable max
location: class abc
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 31]
Error: cannot find symbol
symbol: method fabs(double)
location: class abc
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 31]
Error: non-static method f(double) cannot be referenced from a static context
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 31]
Error: cannot find symbol
symbol: variable EPS
location: class abc
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 32]
Error: non-static method f(double) cannot be referenced from a static context
File: C:\Users\Evil Weevil\Desktop\KasperskyInternetSecurity2012-TrialReset-by-GANJiN-Nov2011\mp\mypros\mypros\abc.java [line: 32]
Error: non-static method f(double) cannot be referenced from a static context

Those messages are mostly clear enough. "cannot find symbol xxx" means you have tried to use a variable xxx but you haven't declared it (or you declared it in the wrong place). The errors referring to "static" are what stultuske warned you about in his last post.

error 1: your variable max is not in the scope where you are using it, put it in the right method (variable max, used on line 29, both stated in your error message)
error 2: you don't have a fabs method (fabs method you're calling on line 31, as stated by your error message)
error 3: your method f should be static (same: all info required to solve this, stated in the error message)
error 4: same as with your max variable, in the wrong scope
error 5: exactly the same as error 3

import java.lang.*;
import java.util.*;
import java.io.*;
public class abc
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{


double EPS=0.01;
double a,b,root;


System.out.println("enter 2");
System.out.println("A :");
a=console.nextDouble();
System.out.println("B :");
b=console.nextDouble();


root = bisect(a,b);


System.out.println("root is "+root);
}



public static double bisect(double a,double b){
int n;
double c;
double EPS=0.01;
int max=5;
for(n=1;n<max;n++){
c=(a+b)/2.0;
if(fabc(f(c))<EPS)        break;
if(f(a)*f(c)<0.0)        b=c;
else a=c;
}
return c;
}
static  double f(double x)
{
return ((x*x*x)-(3*x)+1);
}
}



...................................................
C:\Documents and Settings\Burhan\My Documents\abc.java:41: cannot find symbol
symbol  : method fabc(double)
location: class abc
if(fabc(f(c))<EPS)        break;
^
1 error


Process completed.

and ... what is the point of this reply? to show him that being lazy pays off?
to tell him that he'll always find some sucker to do his work for him?

we already gave him (more than) everything he needs to solve all his problems.
if doing that little bit of effort that was still needed was too much for him, coming back to check on your code and solving the problem that is still there will also be too much.

Was that intended to be helpful code - all that uncommented unindented unexplained code with a compile error? What was the point?

nothing just i wanna know how that will work :S

how what will work? so your post was not ment as an answer to the OP's question?

if you have a question, you could always start a new thread of your own and ask it.

i need example for java bisection method
??? any help

so, you haven't done anything, haven't made the smallest effort to write it yourself, but you do expect us not only to learn what your code should do (because, guess what, not all of us are mathematicians) and write it for you?

I'll give you a big help, even though I know I shouldn't. follow this link, it's the first link ye' olde Google gives you if you do a search for bisection, and, guess what?

It even has the pseudocode written out for you.

so, take a look at that, write it in java, if it doesn't work and you don't know why
1. start your own thread
2. show us what you've done so far (and not just copy paste someone else's code)
3. tell us what 's going wrong (or what you think is going wrong) and point us to the place in your code where it goes wrong
4. show us all relevant information, for instance: a complete copy of the stacktrace is a lot more informative than 'hey, I just got a nullpointerexception'

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.