Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am actually getting " <identifier> expected " error for parameters passed in methods.


What I have tried:

import java.util.Scanner;
import java.lang.Math;

 

class Quad 
{
    int a,b,c;
    float d,x1,x2;
    
    void discriminant( a, b, c)
    {
        
        d = b*b - 4*a*c;
        
        if(d<0)
        {
            System.out.println("Roots are imaginary.");
        }
        else if (d>0)
        {
            System.out.println("Roots are real and unequal. ");
            roots(a,b,c);
        }
        else
        {
            System.out.println("Roots are real and equal. ");
            roots(a,b,c);
        }    
            
        
    }
    
    void roots(a, b, c)
    {
        
        x1 = (-b - Math.sqrt(d))*0.5;
        x2 = (-b + Math.sqrt(d))*0.5;
        
        System.out.println("Roots of the equation are " + x1 + "and " + x2 + " .");
    }
}

 

public class Program3
{
    
    public static void main(String[] args) 
    {
        int a1, a2, a3;
        
        Quad obj = new Quad();
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the value of a :");
        a1 = sc.nextInt();
        
        System.out.println("Enter the value of b :");
        a2 = sc.nextInt();
        
        System.out.println("Enter the value of c :");
        a3 = sc.nextInt();
        
        obj.discriminant(a1,a2,a3);
        sc.close();
       
    }
}
Posted
Updated 17-Jul-21 21:27pm

1 solution

You need to declare the parameter types in your function definitions:
Java
void discriminant( a, b, c) // the compiler cannot guess the types of a, b and c

// correct way
void discriminant(int a, int b, int c) // tell the compiler the types of the inputs

And the same for the roots function. Although you probably need to make all variables float type for consistency.
   
Comments
Hemil Shah 2021 18-Jul-21 3:41am
   
Thanks for the quick response !

I actually did that at first but it seemed to be didn't work again, maybe because of some other errors as well. Nonetheless it working now! Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900