Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Stuck on this code...

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 12-06-2012, 05:21 PM PM User | #1 kron1cd New to the CF scene   Join Date: Dec 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts Stuck on this code... Hello I am still very new to java and I've been looking over this code for quite some time. There are two hurdles I am trying to get by with which the later one actually just started happening recently... I am trying to post the largest and smallest index number from the last two methods. The second thing that is happening is that now my code just hangs after I input the rainfall numbers.... any help would be greatly appreciated!! import java.util.Scanner; import java.text.DecimalFormat; public class Rainfall { public static void main(String[] args) { final int MONTHS = 12; double[] rain = new double[MONTHS]; initRain(rain); double total = totalRain(rain); double average = averageRain(rain, total); double most = mostRain(rain); double least = leastRain(rain); DecimalFormat digit = new DecimalFormat("#.0"); System.out.println("The total rainfall of the year is " + digit.format(total)); System.out.println("The average rainfall of the year is " + digit.format(average)); System.out.println("The month with the highest amount of rain is " + most); System.out.println("The month with the lowest amount of rain is " + least); } public static void initRain(double[] array) { Scanner keyboard = new Scanner(System.in); for (int x = 0; x < array.length; x++) { System.out.print("Enter Rainfall for month " + (x + 1) + ": "); array[x] = keyboard.nextDouble(); } } public static double totalRain(double[] array) { double total = 0; for (int x = 0; x < 12; x++) total += array[x]; return total; } public static double averageRain(double[] array, double total) { return total / array.length; } public static double mostRain(double[] array) {; double maximum = array[1]; int value = 0; for (int i=0; i < 12; i = i++) { if (array[i] >= maximum) maximum = array[i]; value = i; } return value; } public static double leastRain(double[] array) { double minimum = array[0]; int value; for (int i=0; i < 12; i++) { if (array[i] <= minimum) minimum = array[i]; value = i; } return value; } }
 12-06-2012, 06:46 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,603 Thanks: 4 Thanked 2,598 Times in 2,567 Posts You don't need to put that much work into the least and most methods. Instead, sort the arrays and pull from the first and last: PHP Code: ```     public static double mostRain(double[] array)    {        double[] copy = array.clone();        Arrays.sort(copy);        return copy[copy.length - 1];    }        public static double leastRain(double[] array)    {        double[] copy = array.clone();        Arrays.sort(copy);        return copy[0];    }  ``` To get the month index for the most and least would be similar to what you have, but you don't take the values of. PHP Code: ```     public static int mostRainMonth(double[] array)    {        int iMonth = 0;        for (int i = 1; i < array.length; ++i)        {            if (array[i] > array[iMonth])            {                iMonth = i;            }        }        return iMonth;    }    public static int leastRainMonth(double[] array)    {        int iMonth = 0;        for (int i = 1; i < array.length; ++i)        {            if (array[i] < array[iMonth])            {                iMonth = i;            }        }        return iMonth;    }  ``` Conflicting months where two match cannot be reported this way. The hang is an infinite loop here: ` for (int i=0; i < 12; i = i++)`. Remove the assignment (or just use what I posted since I don't use a loop in that counter). I'd give the averageRain an overload as well: PHP Code: ```     public static double averageRain(double[] array)    {        return averageRain(array, totalRain(array));    }  ``` Also, in the future please choose a better topic title to describe the problem, and use [php][/php] or [code][/code] tags around code to preserve the formatting.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:18 AM.