Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Beginning Java
Sorting 2D Java Array
Noah Stahl
Greenhorn
Posts: 4
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
So for this project in school I need to sort a 2D array to look like this,
http://imgur.com/ZX7EQWP
.
Here is the code that I have.
File 1:
import java.text.DecimalFormat; public class Assignment1 { public static void main(String[] args) { String[][] rainArray = { {"January", "3.03"}, {"February", "2.65"}, {"March", "3.79"}, {"April", "3.56"}, {"May", "3.71"}, {"June", "3.43"}, {"July", "4.35"}, {"August", "3.50"}, {"September", "3.78"}, {"October", "3.18"}, {"November", "2.99"}, {"December", "3.56"} }; DecimalFormat myFormatter = new DecimalFormat("##0.00"); //DecimalFormat myFormatTotal = new DecimalFormat("##0.0"); RainFall myRain = new RainFall(rainArray); System.out.println("The total rain is: " + myRain.getTotal() + "."); System.out.println("The average rain is: " + myFormatter.format(myRain.getAverage()) + "\"" + "."); System.out.println("The most amount of rain was in: " + RainFall.getLeastMonth() + "," + " which had " + RainFall.getLeastValue() + "\""); System.out.println("The most amount of rain was in: " + RainFall.getMostMonth() + "," + " which had " + RainFall.getMostValue() + "\""); System.out.println(); RainFall.setOrder(rainArray); } }
File 2:
public class RainFall { private static String[][] rainArray; public RainFall(String[][] rainArray) { this.rainArray = rainArray; } public String getTotal() { double sum = 0; int sumFoot = 0; int sumInches = 0; String finalSum = ""; for (int i = 0; i < rainArray.length; i++) { sum += Double.parseDouble(rainArray[i][1]); } sumFoot = (int) (sum / 12); sumInches = (int) (sum % 12); finalSum = (sumFoot + "\'" + sumInches + "\""); return finalSum; } public double getAverage() { double sum = 0; for (int i = 0; i < rainArray.length; i++) { sum += Double.parseDouble(rainArray[i][1]); } double average = (sum / rainArray.length); return average; } public static double getLeastValue() { double leastValue = Double.parseDouble(rainArray[0][1]);; String leastMonth = rainArray[0][0]; for (int i = 0; i < rainArray.length; i++) { if(leastValue > Double.parseDouble(rainArray[i][1])){ leastValue = Double.parseDouble(rainArray[i][1]); leastMonth = rainArray[i][0]; } } return leastValue; } public static String getLeastMonth() { double leastValue = Double.parseDouble(rainArray[0][1]);; String leastMonth = rainArray[0][0]; for (int i = 0; i < rainArray.length; i++) { if(leastValue > Double.parseDouble(rainArray[i][1])){ leastValue = Double.parseDouble(rainArray[i][1]); leastMonth = rainArray[i][0]; } } return leastMonth; } public static double getMostValue() { double mostValue = Double.parseDouble(rainArray[0][1]);; for (int i = 0; i < rainArray.length; i++) { if(mostValue < Double.parseDouble(rainArray[i][1])){ mostValue = Double.parseDouble(rainArray[i][1]); String mostMonth = rainArray[i][0]; } } return mostValue; } public static String getMostMonth() { double mostValue = Double.parseDouble(rainArray[0][1]);; String mostMonth = rainArray[0][0]; for (int i = 0; i < rainArray.length; i++) { if(mostValue < Double.parseDouble(rainArray[i][1])){ mostValue = Double.parseDouble(rainArray[i][1]); mostMonth = rainArray[i][0]; } } return mostMonth; } public static String setOrder(){ int minIndex, minValue; for(int i = 0; i < (rainArray.length - 1); i++) { minIndex = i; minValue = rainArray[i][]; for(int x = i + 1; x < rainArray.length; x++) { if(rainArray[x][] < minValue) { minValue = rainArray[x]; minIndex = x; } } rainArray[minIndex] = rainArray[i]; rainArray[i] = minValue; } } }
Ulf Dittmer
Rancher
Posts: 43081
77
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch.
Do you have a question about the problem or the code?
Noah Stahl
Greenhorn
Posts: 4
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well it was about the code, but I did end up figuring it out. Thanks!
Campbell Ritchie
Marshal
Posts: 79964
396
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome again. I presume you figured out how bad arrays are for that sort of thing, and how you ought to have created a Rainfall class. That can implement the Comparable interface.
Aaron Morison
Greenhorn
Posts: 1
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Could someone figure out how to get the last bit to output his image?
public static String setOrder(){ int minIndex, minValue; for(int i = 0; i < (rainArray.length - 1); i++) { minIndex = i; minValue = rainArray[i][]; for(int x = i + 1; x < rainArray.length; x++) { if(rainArray[x][] < minValue) { minValue = rainArray[x]; minIndex = x; } } rainArray[minIndex] = rainArray[i]; rainArray[i] = minValue; } }
Campbell Ritchie
Marshal
Posts: 79964
396
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch
I added
code tags
to yoru code. Doesn't it look better.
Does this tiny ad smell okay to you?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Exceptions problem
Drawing in a loop
ArrayList Exceptions
standard deviation
Operations on data in a JTable
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...