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 23 of 23

Thread: compile error

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default compile error

    i dont know whats wrong
    public class prgmTWO
    {
    	public static void main(String [] args)
    	{
    		double P = Input.getInt("How much have you deposited?"); 
    		double i = Input.getInt("What is your annual interest rate?"); 
    		i /= 100;
    		double n = Input.getInt("How many years will it be in the bank?");
    		double s; 
    	    double invoke = bank(P, n, i, s);
    	    System.out.println(invoke);
    	}	    
    	    public static double bank(double P, double n, double i, double s)
    	    {
    	    	double para = 1 + i;
    	    	double intrest = Math.pow(para, n);
    	    	s = P * intrest;
    	    	return s;
    	    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: compile error

    Does that code compile? if not, and you can't understand the compiler messages, post them and someone is sure to explain what they mean.

    It would be a very good plan to change your code to use standard Java coding conventions. Classes begin with a capital letter an variables with a lowercase one. Variables should be descriptive using camelCase where needed. So, eg, if you wanted a variable to stand for the amount of money deposited principal would be better than P, an interest rate is more clearly denoted with interestRate than with i.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: compile error

    wats "Input" in here......
    post ur entire code.....

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: compile error

    Quote Originally Posted by rougeking View Post
    wats "Input" in here......
    post ur entire code.....
    @rougeking, please make an attempt to use proper grammer - this is a technical forum. Posting text message shorthand can make posts not only that much harder to read but also add confusion

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    input.class?

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    heres the errorScreen Shot 2013-01-11 at 1.39.01 PM.jpg
    sorry its small

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

    Default Re: compile error

    Can you copy the console contents and paste it here. The image is too small to see the text,

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    Last login: Mon Jan 7 11:11:36 on ttys000
    petes-MacBook-Air:~ pkrasinski$ cd java\ programs/
    petes-MacBook-Air:java programs pkrasinski$ javac prgmTWO.java
    prgmTWO.java:10: variable s might not have been initialized
    double invoke = bank(P, n, i, s);
    ^
    1 error
    petes-MacBook-Air:java programs pkrasinski$

    --- Update ---

    the arrow is under the "s"

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

    Default Re: compile error

    variable s might not have been initialized
    The compiler wants the variable s to be assigned a value. Can you do that before it is used?


    BTW passing a variable with no meaningful value to a method does not make sense. If you need a variable in the method, define it in the method.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    i got it to work
    new code
    public class prgmTWO
    {
    	public static void main(String [] args)
    	{
    		double P = Input.getInt("How much have you deposited?"); 
    		double i = Input.getInt("What is your annual interest rate?"); 
    		i /= 100;
    		double n = Input.getInt("How many years will it be in the bank?"); 
    	    double invoke = bank(P, n, i);
    	    System.out.println("Intrest earned = $ " + invoke);
    	}	    
    	    public static double bank(double P, double n, double i)
    	    {
    	    	double para = 1 + i;
    	    	double intrest = Math.pow(para, n);
    	    	double s = P * intrest;
    	    	return s;
    	    }
    }
    how do i get the output to be 2 decimals only?

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

    Default Re: compile error

    get the output to be 2 decimals only?
    See the printf() method
    or the DecimalFormat class
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    new program
    public class prgmONE // Peter Krasinski
    {
    	public static void main(String [] args)
    	{
    		int c = 0, f = 0;
    		int c2 = cels(c, f);
    		int which = Input.getInt("1 for Celsius, 2 for Fahrenheit"); 
    		switch (which)
    		{
    			case 1: 
    			f = Input.getInt("Degree Fahrenheit?");
    			System.out.println(f + "F is " + c2 + "C");  
    				break;
    			case 2:
    			c = Input.getInt("Degree Celsius?");
    			System.out.println(c + "C is "  + "F"); 
    				break;	
    		}
    	}
    		public static int cels(int f,int c)
    		{
    			c = ((9/5) * (f + 32));
    			return c;	
    		}
    }

    don't know why its not returning the proper value from the cels method. im half way done and wanted to see if the cels method worked
    maybe i should sleep on it, that usually works

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

    Default Re: compile error

    Print out the value of this: (9/5) to see what integer arithmetic does.
    Then change one of the numbers to a double and see what is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    Its sill not returning the proper value
    public class prgmONE // Peter Krasinski
    {
    	public static void main(String [] args)
    	{
    		double c = 0, f = 0;
    		double c2 = cels(c, f);
    		int which = Input.getInt("1 for Celsius, 2 for Fahrenheit"); 
    		switch (which)
    		{
    			case 1: 
    			f = Input.getDouble("Degree Fahrenheit?");
    			System.out.println(f + "F is " + c2 + "C");  
    				break;
    			case 2:
    			c = Input.getDouble("Degree Celsius?");
    			System.out.println(c + "C is "  + "F"); 
    				break;	
    		}
    	}
    		public static double cels(double f, double c)
    		{
    			double b = 9/5;
    			c = b * (f + 32);
    			return c;	
    		}
    }
    i don't think its returning c

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

    Default Re: compile error

    Did you try the tests I suggested in post #13?
    What printed out?

    Print out the value of b in the cels() method to see what its value is.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    its 0 how do i change it

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

    Default Re: compile error

    That's strange. I expected it to be 1.0?
    Can you post the code and the code you used to print it?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    nvm i did 5/9. your right i get 1.0

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

    Default Re: compile error

    What you are seeing is the results of integer arithmetic: 9/5 = 1 (no decimal places)
    Change one of the numbers to a double: 9.0/5 and see what you get.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    gottcha so i should do it in the other program

    --- Update ---

    if i put 55 in for fahrenheit i get 57.6 (it should be 12.7)
    public class prgmONE // Peter Krasinski
    {
    	public static void main(String [] args)
    	{
    		double c = 0, f = 0;
    		double c2 = cels(c, f);
    		int which = Input.getInt("1 for Celsius, 2 for Fahrenheit"); 
    		switch (which)
    		{
    			case 1: 
    			f = Input.getDouble("Degree Fahrenheit?");
    			System.out.println(f + "F is " + c2 + "C");  
    				break;
    			case 2:
    			c = Input.getDouble("Degree Celsius?");
    			System.out.println(c + "C is "  + "F"); 
    				break;	
    		}
    	}
    		public static double cels(double f, double c)
    		{
    			double b = 9.0/5;
    			c = b * (f + 32);
    			return c;	
    		}
    }

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

    Default Re: compile error

    Are the equations correct? Check that all the computations are generating the expected values.
    Add some println statements that print out all the intermediate results so you can see where the computation is going wrong.

    For example in the cels() method print out the values of f and c that are passed to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member
    Join Date
    Sep 2012
    Posts
    42
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: compile error

    i think cels method is pulling f as 0 not from the case 1

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

    Default Re: compile error

    How can the value of f from case 1 be used?

    Executed code uses the values of variables when the code is executed.
    When is the cels() method called? What are the values of c and f when it is called?
    When is f given a value?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Please compile this program and see if it gives you an error
    By Programming_Hobbyist in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:36 PM
  2. Java 2D Example compile error
    By weziw in forum Java SE APIs
    Replies: 1
    Last Post: April 2nd, 2012, 07:57 AM
  3. Error while compile
    By when im gone in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 7th, 2012, 10:53 AM
  4. [Compile Error] Whats wrong with that ?
    By Anomis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 11:01 AM
  5. Compile Error, tried everything please help
    By cdub0 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 03:31 AM