Results 1 to 14 of 14
Thread: Summarize an Java Array?
- 11-03-2012, 02:51 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Summarize an Java Array?
Hello people of Java! :-)
Here you have a fellow brother in need of assistance!
I´m currently trying to make a program which takes input from the user and saves the input into an array.
The array will then post the sum & the median of all the numbers in the array.
Here is my code:
I thank you greatly for your assistance!Java Code:import java.util.Arrays; import java.util.Scanner; public class Class3 { public static void main(String[] args) { int talett, taltva, taltre, talfyra, talfem, summa, medeltal; Scanner user_input = new Scanner(System.in); System.out.println("Skriv in 5 nummer"); int [] list = new int[5]; // Here I create my list. for (int index = 0; index < list.length; index++) list[index] = user_input.nextInt(); // Here I take input from the user from which the input is saved to the array. // Here I would like to have some code which sums the numbers of the array and posts it on the screen. System.out.println(Arrays.toString(list)); }Last edited by C.phantasy; 11-03-2012 at 03:07 AM.
-
Re: Summarize an Java Array?
Please use [code] [/code] tags when posting code, not [quote] [/quote] tags since the former makes your code retain its formatting and thus be readable. Please also go into a bit more depth about just where you're stuck.
- 11-03-2012, 03:09 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
Thanks for a quick reply!
I´m getting stuck at the part where I´m supposed to summarize the array values.
I do not know what to write since traditional "sum += nameofarray;" command won´t work.
-
Re: Summarize an Java Array?
Thanks for the edit. Now your code is easier to read.
Anyway, to summarize the values in the array, simply create a sum variable, loop through the array with a for loop, and add each item to the sum variable. Note that all of your code blocks be they one line for loops or one line if blocks, should be enclosed within curly braces for readability and for safety.
- 11-03-2012, 03:15 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
Thanks yet again!
Could you perhaps show me an example in my code? I´ve been stuck with this problem for about 3 hours and I´m really feeling confused about what I should do.
-
Re: Summarize an Java Array?
- 11-03-2012, 03:28 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
Allright, I will give it a shot in the morning :-)
- 11-03-2012, 01:45 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
Here is my new code:
I have highlighted where the problem arises.Java Code:import java.util.Arrays; import java.util.Scanner; public class Class1 { public static void main(String[] args) { int talett, taltva, taltre, talfyra, talfem, summa, medeltal; summa = 0; Scanner user_input = new Scanner(System.in); System.out.println("Write 5 numbers"); int [] list = new int[5]; // Here I create my list. for (int index = 0; index < list.length; index++) list[index] = user_input.nextInt(); // Here I take input from the user from which the input is saved to the array. summa +=list[index]; // [B]Here is the problem: index is not recognized as an field?[/B] System.out.println(Arrays.toString(list)); System.out.println(summa); } }
I do not really know if the problem perhaps is related to that Java can not handle input from a user to an array and then summarize the numbers which the user put in?Last edited by C.phantasy; 11-03-2012 at 01:52 PM.
- 11-03-2012, 02:12 PM #9
Re: Summarize an Java Array?
The loop variable index is in scope only inside the loop, and due to the absence of braces {} only one line is inside the loop.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-03-2012, 03:49 PM #10
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
I´m going to admit that I feel really stupid at the moment due to the fact that the book i use do not explain the use of {} properly. I´m at page 402 and there has not been a proper explaination yet...
Please correct my code and perhaps I will understand?
Ps. No, I´m not here to be lazy and ask people to correct my code all the time.
Edit:
I solved it! Thanks for your help!
Java Code:import java.util.Arrays; import java.util.Scanner; public class Class1 { public static void main(String[] args) { int talett, taltva, taltre, talfyra, talfem, summa, medeltal; summa = 0; Scanner user_input = new Scanner(System.in); System.out.println("Skriv in 5 nummer"); int [] list = new int[5]; // Here I create my list. for (int index = 0; index < list.length; index++) { list[index] = user_input.nextInt(); // Here I take input from the user from which the input is saved to the array. summa += list[index];} System.out.println(summa); // Here I would like to have some code which sums the numbers of the array and posts it on the screen. System.out.println(Arrays.toString(list)); } }Last edited by C.phantasy; 11-03-2012 at 03:52 PM.
- 11-03-2012, 04:41 PM #11
Re: Summarize an Java Array?
Good show, and you're welcome. Oh, and there's no call to feel stupid. Never confuse ignorance with stupidity. There's no discredit in being ignorant of something and subsequently removing that ignorance through learning.
Now that you've discovered the significance of braces to form code blocks, learn the coding conventions so you know where the braces should be places in well formatted code: Code Conventions for the Java Programming Language
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-03-2012, 05:05 PM #12
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Re: Summarize an Java Array?
Thanks db! I feel more comfortable hearing this from from you
.gif)
I will read the Code convention and try to use it regulary!
Ps. I would like to have your aid on another problem which is connected to finding the maximum value in an array:
Here is my code:
The problem is that I can run the program but it crashes with following message:Java Code:import java.util.Arrays; import java.util.Scanner; import java.lang.Math; public class Class1 { public static void main(String[] args) { int störstatalet, summa, medeltal, antalposter; summa = 0; Scanner user_input = new Scanner(System.in); System.out.println("Hur många poster skall finnas i listan?"); antalposter = user_input.nextInt(); System.out.println("Skriv in " + antalposter+" nummer"); int [] list = new int[antalposter]; // Här skapar jag listan "list" som kommer bestå av heltal(int) med 5 st poster for (int index = 0; index < list.length; index++) { // Viktigt att ha med {} för att inkludera allt i for -satsen. list[index] = user_input.nextInt(); summa = summa + list[index]; Arrays.sort(list);} // Here I sort the values from the list. medeltal = summa / antalposter; störstatalet = list[antalposter]; // Here I try to make the program pick the last post in the list since it must have the greatest value. System.out.println("Talen du valt är:" + Arrays.toString(list)); System.out.println("Summan av talen är:" + summa); System.out.println("Medeltalet blir:"+ medeltal); System.out.println("Största talet=" + störstatalet); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Class1.main(Class1.java:31)
I guess this means that the problem is related to the index value I chose is out of reach? What do you suggest I should consider?Last edited by C.phantasy; 11-03-2012 at 05:10 PM.
- 11-04-2012, 12:08 AM #13
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Summarize an Java Array?
antalposter is set to whatever number the person puts in.... so I enter "4" in response to "Hur många poster skall finnas i listan?" antalposter = 4 ...
list[4] is out of bounds! so
Java Code:störstatalet = list[antalposter -1];
- 11-04-2012, 10:28 PM #14
Member
- Join Date
- Nov 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Java 2D Array
By The Dark Dragon in forum New To JavaReplies: 3Last Post: 11-27-2011, 08:00 AM -
`java array
By shane123 in forum New To JavaReplies: 18Last Post: 03-22-2010, 12:42 AM -
Java Array Help
By md69holla in forum New To JavaReplies: 14Last Post: 02-15-2010, 06:02 PM -
How to transfer 1D array in JAVA to 3D array in C
By fishwater00 in forum New To JavaReplies: 0Last Post: 07-31-2009, 06:24 PM -
how to convert a Java array to a java stack?
By pompeez in forum New To JavaReplies: 2Last Post: 08-13-2007, 02:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks