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

Thread: Found String Requires Double Problem.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Found String Requires Double Problem.

    I am writing a program that allows the user to create an array of 12 numbers and my program has the ability to find the highest month's rainfall, the average rain fall, and the total rainfall. The total and average rainfall work fine but in the getHighestMonth() method i continually have the issues of my variable month being found as a java.lang.String but it requires a double. The highest month method is supposed to display the actual month's name and not the amount of rainfall.

    Thanks for the help.

     public class RainFall
       {  
    		public double[] values; 		
       // Constructor method
           public RainFall(double[] d)
          {
    			values = new double[d.length];
     
    			for (int index = 0; index < d.length; index++)
    				values[index] = d[index];
          }
     
       // Return the sum total of values in the array
           public double getTotalRainFall()
          {
             double total = 0.0;
     
    			for(double val : values)
             total = total + val;
             return total;
          }
     
     
       // Return the average (mean) of the values in array
           public double getAverageRainFall()
          {
             double mean = 0.0;
    			int count = 0;
     
            	mean = getTotalRainFall()/values.length;
     
             return mean;
          }
     
     
     
    		public double getHighestMonth()
    		{
    			double highest = values[0];
    			String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December"};
    			String month = "";
     
    			for (int index = 0; index < values.length; index++)
    			{
    				if (values[index] > highest)
    					month = months[index];
    					highest = values[index];
    			}
    			return month;
    		}
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Found String Requires Double Problem.

    		public String getHighestMonth() //Changed from double to String.. you were saying it should have a 
                                                        //return type of double, but returning a String
    		{
    			double highest = values[0];
    			String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December"};
    			String month = "";
     
    			for (int index = 0; index < values.length; index++)
    			{
    				if (values[index] > highest)
    					month = months[index];
    					highest = values[index];
    			}
    			return month;
    		}
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    NPotter86 (January 17th, 2011)

Similar Threads

  1. application id not found
    By pradeepsetty in forum Java IDEs
    Replies: 3
    Last Post: June 2nd, 2010, 02:37 AM
  2. Jsp source not found
    By jadeite100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 15th, 2010, 12:00 AM
  3. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM
  4. Main Class Not Found Problem
    By shadow in forum Java Theory & Questions
    Replies: 3
    Last Post: September 29th, 2009, 09:42 AM
  5. String and int problem in swing program
    By duckman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2009, 02:28 AM