Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default adding numbers in an array

    hi! so i'm new to this forum but i found it helpful in some of my java problems.

    so you see i need to add numbers that i have entered in an array. i have tried using a for loop and i know i must use it. my problem is, that when i would like to see the total sum i see different totals for each numbers. example: 97, 98, 54, 28 (numbers entered in array) and then the totals: 195, 249, 277.

    is there a way i can see only 1 full total?

    thanks

    class ProgramNo23{
    public static void main (String args[]){

    String names [] = new String [10];
    int marks [] = new int [10];
    int size = 0;
    int option = 0;
    int sum = 0;

    do{

    System.out.println("1. Enter Name & Mark");
    System.out.println("2. Edit a Mark");
    System.out.println("3. Display Name, Marks & Grades");
    System.out.println("4. Display Statistics");
    System.out.println("5. Display Passes");
    System.out.println("6. Display Failures");
    System.out.println("7. Exit");

    System.out.println();
    System.out.println("---------------------------------");
    System.out.println();

    System.out.print("Enter option: ");
    option = Keyboard.readInt();

    System.out.println();

    switch(option){

    case 1: // Enter Names & Marks

    for(int i = 1; i < 11; i++){

    System.out.print("Please Enter Name "+i+": ");
    String n = Keyboard.readString();

    System.out.print("Please Enter Mark "+i+": ");
    int m = Keyboard.readInt();

    System.out.println();

    names[size] = n;
    marks[size] = m;

    size++;
    }

    break;

    case 2: // Edit a Mark

    {
    if(size!=0)
    {
    boolean found = false;

    System.out.println("Enter name to edit: ");
    String m = Keyboard.readString();

    System.out.println();

    for(int p = 1;p < size; p++){

    if(names[p].equalsIgnoreCase(m)){

    System.out.println("Name Found");

    System.out.println();

    System.out.print("Enter new mark: ");
    int newMark = Keyboard.readInt();

    System.out.println();
    }
    }

    if(found == false) System.out.println("Name not found");
    System.out.println();
    }

    else System.out.println("Directory Empty");

    break;
    }

    case 3: // Display Name, Mark and Grade

    {
    for(int x = 0; x < size; x++){
    System.out.println(names[x]+": ");

    if(marks[x] >= 90) System.out.print("Grade A: ");
    if((marks[x] >= 80)&&(marks[x] < 90)) System.out.print("Grade B: ");
    if((marks[x] >= 70)&&(marks[x] < 80)) System.out.print("Grade C: ");
    if((marks[x] >= 60)&&(marks[x] < 70)) System.out.print("Grade D: ");
    if((marks[x] >= 50)&&(marks[x] < 60)) System.out.print("Grade E: ");
    if(marks[x] < 50) System.out.print("Grade F: ");

    System.out.println(marks[x]);
    System.out.println();

    }

    System.out.println();
    }
    break;

    case 4: // Display Statistics

    for(int s = 0; s < size; s++){

    sum = sum + marks[s];

    System.out.print(sum);

    System.out.println();
    }

    break;
    }

    }

    while(option!=7);
    }
    }

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

    Default Re: adding numbers in an array

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

  3. #3
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: adding numbers in an array

    whats happening is your println(sum) is inside your for loop, so everytime your for loop executes your getting a print out. just move it out of the loop but still inside of that switch statement and you should be good to go

    Java Code:
    case 4: // Display Statistics
    
    for(int s = 0; s < size; s++){
    
    sum = sum + marks[s];
         }
    
    System.out.print(sum);
    // not sure what this println() with empty value is for? maybe to skip a line, but i added with your other print because 
    // i assume they belong together 
    System.out.println();   
    break;

  4. #4
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: adding numbers in an array

    thx a lot :D ! problem solved!

Similar Threads

  1. adding certain numbers from multi-dimensional array
    By sirganon in forum New To Java
    Replies: 5
    Last Post: 03-31-2012, 09:11 PM
  2. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  3. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 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
  •