woodpecker 0 #1 December 2, 2006 Anyone have any experience with arrays in java? I have some coding to do and a little stuck. v/r Billy Have I mentioned that I hate JAVA? 4 more weeks....SONIC WOODY #146 There is a fine line between cockiness and confidence -- which side of the line are you on? Quote Share this post Link to post Share on other sites
unformed 0 #2 December 2, 2006 what are you trying to do? dude, java makes arrays easy .... if you're in cs, you're going to end up using them constantly...This ad space for sale. Quote Share this post Link to post Share on other sites
woodpecker 0 #3 December 2, 2006 I'm trying to write a program that will read a list of values, one per line, and outputs their sum. Then each value will be printed with its percentage of the sum. I'm using a method that will take the entire array as one argument and return the sum of the array. And Now I'm stuck. Any ideas? I can post the coding I have so far if it will help. here is what the program is supposed to look like running. How many values? 4 Enter value: 4 Enter value: 3 Enter value: 5 Enter value: 6 The sum is 18 Percentages: 4 is 22.2% of 18 3 is 16.7% of 18 5 is 27.8% of 18 6 is 33.3% of 18 I HATE JAVA SONIC WOODY #146 There is a fine line between cockiness and confidence -- which side of the line are you on? Quote Share this post Link to post Share on other sites
jsaxton 0 #4 December 2, 2006 http://java.about.com/od/beginningjava/l/aa_array.htm Quote Share this post Link to post Share on other sites
dmace 0 #5 December 2, 2006 Post the code you have. Let's see what we can do with it Quote Share this post Link to post Share on other sites
woodpecker 0 #6 December 2, 2006 import java.util.*; public class value { public static void main(String [] args) { Scanner stdin = new Scanner(System.in); int numberOfValues; System.out.println("How many values? "); numberOfValues = stdin.nextInt(); int [] numbers = new int [numberOfValues]; int sum; for(int 1=0; i < numbers.length; i++) { System.out.println("Enter a value: "); numbers = stdin.nextInt(); } sum = getSum(numbers); System.out.println("The sum is " + sum); } -------------------this is where I get lost. Not sure how to add the coding into the getSum method so the sum is calculated from the array. public static int getSum(int [] list) { int s; s = list[0]; ??? ??? ??? return s; } }SONIC WOODY #146 There is a fine line between cockiness and confidence -- which side of the line are you on? Quote Share this post Link to post Share on other sites
unformed 0 #7 December 2, 2006 here's a quick solution. there might be some typos, but it should work fine. void printData(int data[]) { int sum=0; for(int i=0; isum+=data[.i]; DecimalFormat df = new SimpleDecimalFormat("0.0"); for(int i=0; idouble percentage = (100.0*data[.i])/sum; System.out.println(data[.i] + " is " + df.format(percentage) + "% of " + sum; } } (Take the dots out after the brackets)This ad space for sale. Quote Share this post Link to post Share on other sites
dmace 0 #8 December 2, 2006 try this: import java.util.*; public class value { public static void main(String [] args) { Scanner stdin = new Scanner(System.in); int numberOfValues; System.out.println("How many values? "); numberOfValues = stdin.nextInt(); int [] numbers = new int [numberOfValues]; int sum; for(int i=0; i < numbers.length; i++) { System.out.println("Enter a value: "); numbers = stdin.nextInt(); } sum = getSum(numbers); System.out.println("The sum is " + sum); } public static int getSum(int [] list) { int iSum = 0; for(int i=0; i < list.length; i++) { iSum = iSum + list; } return iSum; } } weird, replying doesn't show the brackets [] ... should be: numbers BRACKET "i" BRACKET = stdin.nextInt(); and iSum = iSum + list BRACKET "i" BRACKET; Quote Share this post Link to post Share on other sites
mailin 0 #9 December 2, 2006 Out of all the programming languages I have had to learn over the years, JAVA remains my favorite. When you get a bit more experience, you'll see why Arianna Frances Quote Share this post Link to post Share on other sites
MarkM 0 #10 December 2, 2006 Quote I HATE JAVA Then try Perl or another interpreted language sometime. This took about 3 minutes of coding: #!/usr/bin/perl print "Type in your numbers, hit CTRL-D when done:\n"; @numbers = ; foreach $number(@numbers) { chomp $number; $total += $number; } print "Total sum is $total\n"; foreach $number(@numbers) { chomp $number; print "$number is " . $total / $number . "% of $total\n"; } I do like Java though, even though I've rarely been able to use it for anything I've worked on. It was my first OO language and probably one of the better ones to learn on. I'm starting up next month as a Ruby on Rails developer and it'll be interesting to see how well that language works out. Quote Share this post Link to post Share on other sites
woodpecker 0 #11 December 2, 2006 Thanks all for all the great help. Much appreciated and it now works. Thank god this is the only programming class I need and only have 4 more weeks of it. Unless other Object Orienting languages make more sense....I'm done. Now off to the percents. Thanks again all. I owe beer at a dropzone near you! v/r BillySONIC WOODY #146 There is a fine line between cockiness and confidence -- which side of the line are you on? Quote Share this post Link to post Share on other sites
mailin 0 #12 December 2, 2006 hahaha QuoteUnless other Object Orienting languages make more sense....I'm done. Yep, you're done. I won't bother pointing (get it, haha ) you to C++, it'll make your head explode. Good luck with the remaining 4 weeks! JenArianna Frances Quote Share this post Link to post Share on other sites