Results 1 to 14 of 14
  1. #1
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default 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:

    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));
    }
    I thank you greatly for your assistance!
    Last edited by C.phantasy; 11-03-2012 at 03:07 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  5. #5
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default 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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Summarize an Java Array?

    Quote Originally Posted by C.phantasy View Post
    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.
    My philosophy and experience has been that it's always best for you to try first. Knowing what I've suggested, please try to come up with a solution and post it here.

  7. #7
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default Re: Summarize an Java Array?

    Allright, I will give it a shot in the morning :-)

  8. #8
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default Re: Summarize an Java Array?

    Quote Originally Posted by C.phantasy View Post
    Allright, I will give it a shot in the morning :-)
    Here is my new code:
    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 have highlighted where the problem arises.

    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default Re: Summarize an Java Array?

    Quote Originally Posted by DarrylBurke View Post
    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.

    db
    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. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Summarize an Java Array?

    Quote Originally Posted by C.phantasy View Post
    I solved it! Thanks for your help!
    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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default Re: Summarize an Java Array?

    Quote Originally Posted by DarrylBurke View Post
    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

    db
    Thanks db! I feel more comfortable hearing this from from you

    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:
    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);
    
    
            
        }
    }
    The problem is that I can run the program but it crashes with following message:

    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.

  13. #13
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default 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];

  14. #14
    C.phantasy is offline Member
    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Default Re: Summarize an Java Array?

    Quote Originally Posted by SJF View Post
    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];
    Thanks for the assistance!

Similar Threads

  1. Java 2D Array
    By The Dark Dragon in forum New To Java
    Replies: 3
    Last Post: 11-27-2011, 08:00 AM
  2. `java array
    By shane123 in forum New To Java
    Replies: 18
    Last Post: 03-22-2010, 12:42 AM
  3. Java Array Help
    By md69holla in forum New To Java
    Replies: 14
    Last Post: 02-15-2010, 06:02 PM
  4. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  5. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •