Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Calling a function not working

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Calling a function not working

    I have the following function cube_vol() to calculate the volume of a cube. It gives me an error as follows:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method cube_vol(double) in the type Test is not applicable for the arguments ()
    Could you please help me see what went wrong? Thank you.

    import java.util.Scanner;
     
    public class Test {
     
    	public static void main(String[] args) {
     
                    // calling the function
    		cube_vol();
    	}
    		// Cube function
    		public static double cube_vol(double sideCube) {
    			printMe("What is the length of one of the sides: ");
    			Scanner input = new Scanner(System.in);
    			double sideCube = input.nextDouble();
    			double ans1 = cube_vol(sideCube);
    			printMe("The cube's volume is: %.2f\n", ans1);
    			double cubeVol = Math.pow(sideCube, 3);
    			return cubeVol;
    		}
     
    		public static void printMe (String text) {
    			System.out.println(text);
    		}
     
    		public static void printMe (String text, double value) {
    			System.out.printf(text, value);
    		}
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a function not working

    method cube_vol(double) in the type Test is not applicable for the arguments ()
    The cube_vol method is declared to take a double as an argument. The statement with the error does not have any arguments.
    To fix - Add a double value as an argument to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a function not working

    The cube_vol method is asking the user for an input of a double value of the side of the cube.
    double sideCube = input.nextDouble();

    Do I add a double value like this?
    cube_vol(sideCube);

    And it is still not working giving me an error:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    sideCube cannot be resolved to a variable

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Calling a function not working

    sideCube cannot be resolved to a variable
    Is sideCube declared and given a value before it is used?
    You need to post all the code to show where those two statements are located.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling a function not working

    That's the whole piece of code I have. I post it again here.
    sideCube is declared as this:

    double sideCube = input.nextDouble();

    When the user enters a double value, it is the argument for the cube_vol method but this doesn't work.

    import java.util.Scanner;
     
    public class Test {
     
    	public static void main(String[] args) {
     
                    // calling the function
    		cube_vol(sideCube);
    	}
    		// Cube function
    		public static double cube_vol(double sideCube) {
    			printMe("What is the length of one of the sides: ");
    			Scanner input = new Scanner(System.in);
    			double sideCube = input.nextDouble();
    			double ans1 = cube_vol(sideCube);
    			printMe("The cube's volume is: %.2f\n", ans1);
    			double cubeVol = Math.pow(sideCube, 3);
    			return cubeVol;
    		}
     
    		public static void printMe (String text) {
    			System.out.println(text);
    		}
     
    		public static void printMe (String text, double value) {
    			System.out.printf(text, value);
    		}
    	}


    --- Update ---

    Got that working now

    package com.langara.week4;
     
    import java.util.Scanner;
     
    public class Test {
     
    	public static void main(String[] args) {
     
     
    		cube_vol();
    	}
    		// Cube function
    		public static double cube_vol() {
    			printMe("What is the length of one of the sides: ");
    			Scanner input = new Scanner(System.in);
    			double sideCube = input.nextDouble();
    			double cubeVol = Math.pow(sideCube, 3);
    			printMe("The cube's volume is: %.2f\n", cubeVol);
    			return cubeVol;
    		}
     
    		public static void printMe (String text) {
    			System.out.println(text);
    		}
     
    		public static void printMe (String text, double value) {
    			System.out.printf(text, value);
    		}
    	}

Similar Threads

  1. problem: calling a function in Intellij (Beginner)
    By yeezreal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 18th, 2018, 03:05 PM
  2. assert() function is not working. Why?
    By hefese in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 7th, 2014, 01:57 PM
  3. Calling Public Function in JInnternalFrame
    By mfrigon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 31st, 2014, 01:41 PM
  4. What is Wrong When calling the function using arrayList.
    By nutan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2013, 07:30 AM
  5. Working with log math function
    By colerelm in forum Object Oriented Programming
    Replies: 0
    Last Post: October 15th, 2012, 06:30 PM

Tags for this Thread