Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Arraylist of averages of two arrays list

  1. #1
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Arraylist of averages of two arrays list

    I have written the code below the third array is suppose to store the averages of test scores from the first two arrays that were entered, fourth array stores letter grade. My question is do I need to manually write out each value one by one if so how do I do that and if I don't what do I need to do. This is a project and I don't expect the answer but help in the right direction is very much appreciated.

     
    import java.util.*;
     
    public class TestArray
    {
        public static void main (String[] args)
        {
     
    int[] testOne = new int[4];
    int[] testTwo = new int[4];
    double[] average = new double[4];
    char[] grade = {'A', 'B', 'C', 'F'}; // final letter grade
     
    Scanner scan = new Scanner(System.in);
     
     
    // Input scores for both test and all four students.
     
    System.out.println("For test 1,");
    for ( int i = 0; i < testOne.length; i++)
    {
    System.out.print("Enter score" + (i + 1) + ":");
    testOne[i] = scan.nextInt();
    }
     
    System.out.println("For test 2,");
    for ( int i = 0; i < testTwo.length; i++)
    {
    System.out.print("Enter score" + (i + 1) + ":");
    testTwo[i] = scan.nextInt();
    }
     
            System.out.println("");
            System.out.println("Test 1");
     
            for(int i = 0; i < testOne.length; i++)
            {
                System.out.println(testOne[i]);
            }
     
            System.out.println("");
            System.out.println("Test 2");
     
            for(int i = 0; i < testTwo.length; i++)
            {
                System.out.println(testOne[i]);
            }
     
     
     
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Arraylist of averages of two arrays list

    My question is do I need to manually write out each value one by one if so how do I do that and if I don't what do I need to do.
    I guess you're asking about how to figure out the averages. No, you don't need to do this manually. If you did, it would look like:

    average[0] = (testOne[0] + testTwo[0]) / 2;
    average[1] = (testOne[1] + testTwo[1]) / 2;
    average[2] = (testOne[2] + testTwo[2]) / 2;
    average[3] = (testOne[3] + testTwo[3]) / 2;

    But instead of all that use a for loop just as you do when you print the test scores. Instead of 0, 1, 2, 3 use the loop counter variable.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Hazmat210 (April 4th, 2012)

  4. #3
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist of averages of two arrays list

    So in the loop would be written something like this:

     
    for(int i = 0; i < average.length; i++)
    {
    average[i] = (testOne[i] + testTwo[i]) / 2;
    System.out.println(average[i]);
    }


    I am also trying to figure out the letter grade portion of the program should I create a new method with a if else to change each average to a letter grade or is there a way to do that with a loop? Below is what i've started but i realize one array is int and the other is string this is why i am stuck.

     
    if(average[i] >= 90)  
     
    average[i] = grade[0];  
     
    else if (average >= 80)  
     
    average[i] = grade[1];  
     
    else if (average >= 70)  
     
    average[i] = grade[2];  
     
    else if (average < 70)  
     
    average[i] = grade[3];  
     
    return grade;


    also how would I line up the results to look like this:

    Test 1 Test 2 Average Grade
    ------ ------ ------- -----
    090 059 074.5 C
    082 080 081.0 B
    039 100 069.5 F
    089 092 090.5 A
    Last edited by Hazmat210; April 5th, 2012 at 12:21 AM.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Arraylist of averages of two arrays list

    The loop looks good.

    I am also trying to figure out the letter grade portion of the program should I create a new method with a if else to change each average to a letter grade or is there a way to do that with a loop?
    You can figure out the grade char as soon as you know the average. ie inside the loop you have just written.

    A chain of if-else if-... is OK. But the logic of what you have written is a little weird. I *assume* the values of the grade array should be the grade chars assigned to each of the tests. In that case I don't really see where that initialisation way ack at the start came from:

    char[] grade = {'A', 'B', 'C', 'F'}; // final letter grade

    And you would say something like:

    if(whatever) {
        grade[i] = 'A';
    } else if(somethingElse) {
        grade[i] = 'B';
    } // and so on

    -----

    System.out.printf() works well when trying to format numbers. Look up the API docs for that method and follow the links to find out about the format stringss. (There is also the NumberFormat class)

  6. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Hazmat210 (April 5th, 2012)

  7. #5
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist of averages of two arrays list

    I see now setting the grades in the array before knowing the averages makes no sense so array should be empty, but should it be like this:

     
    for(int i = 0; i < average.length; i++)
    		{
    			average[i] = (testOne[i] + testTwo[i]) / 2;
     
    			for(int i = 0; i < grade.length; i++)
    				{			
     
    			if (average[i] >= 90)
    				{
    				grade[i] = 'A';
    				}
    			else (average[i] >= 80)
    				{
    				grade[i] = 'B';
    				}
    			else (average[i] >= 70)
    				{
    				grade[i] = 'C';
    				}
    			else (average[i] < 70)
    				{
    				grade[i] = 'F';
    				}
    		}
    		} 
    	System.out.println("Grades");
    	System.out.println(grade[i]);

    here is the whole code re-written can you please look it over I am still looking at the API docs on format strings.


     
    import java.util.*;
     
    public class TestArray
    {
        public static void main (String[] args)
        {
     
    int[] testOne = new int[4];
    int[] testTwo = new int[4];
    double[] average = new double[4];
    char[] grade = new char[4]; // final letter grade
     
    Scanner scan = new Scanner(System.in);
     
     
    // Input scores for both test and all four students.
     
    System.out.println("For test 1,");
    for ( int i = 0; i < testOne.length; i++)
    {
    System.out.print("Enter score" + (i + 1) + ":");
    testOne[i] = scan.nextInt();
    }
     
    System.out.println("For test 2,");
    for ( int i = 0; i < testTwo.length; i++)
    {
    System.out.print("Enter score" + (i + 1) + ":");
    testTwo[i] = scan.nextInt();
    }
     
            System.out.println("");
            System.out.println("Test 1");
     
            for(int i = 0; i < testOne.length; i++)
            {
                System.out.println(testOne[i]);
            }
     
            System.out.println("");
            System.out.println("Test 2");
     
            for(int i = 0; i < testTwo.length; i++)
            {
                System.out.println(testOne[i]);
            }
     
    	System.out.println("Average");
     
     
    	for(int i = 0; i < average.length; i++)
    		{
    			average[i] = (testOne[i] + testTwo[i]) / 2;
     
    			for(int i = 0; i < grade.length; i++)
    				{			
     
    			if (average[i] >= 90)
    				{
    				grade[i] = 'A';
    				}
    			else (average[i] >= 80)
    				{
    				grade[i] = 'B';
    				}
    			else (average[i] >= 70)
    				{
    				grade[i] <70)
    				{
    				grade[i] = 'F';
    				}
    		}
    		} 
    	System.out.println("Grades");
    	System.out.println(grade[i]);
    }
    }
    Last edited by Hazmat210; April 5th, 2012 at 09:38 AM.

  8. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Arraylist of averages of two arrays list

    Quote Originally Posted by Hazmat210 View Post
    I see now setting the grades in the array before knowing the averages makes no sense so array should be empty, but should it be like this:

     
    for(int i = 0; i < average.length; i++)
    		{
    			average[i] = (testOne[i] + testTwo[i]) / 2;
     
    			for(int i = 0; i < grade.length; i++)
    				{			
     
    			if (average[i] >= 90)
    				{
    				grade[i] = 'A';
    				}
    			else (average[i] >= 80)
    				{
    				grade[i] = 'B';
    				}
    			else (average[i] >= 70)
    				{
    				grade[i] = 'C';
    				}
    			else (average[i] < 70)
    				{
    				grade[i] = 'F';
    				}
    		}
    		} 
    	System.out.println("Grades");
    	System.out.println(grade[i]);
    Hello Hazmat210!
    Some notifications. I think you don't need the inner for loop - you will be ok with one. You should use an if... else if... structure as pbrockway2 mentioned in his last post. It would also better to use intervals in your statements. For example:
    if (average[i] >= 80 && average[i] < 90)
    Hope it helps.

  9. The Following User Says Thank You to andreas90 For This Useful Post:

    Hazmat210 (April 5th, 2012)

  10. #7
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist of averages of two arrays list

    So this below would be enough ? I just thought that a new loop would be necessary in order to set all four values in the grade array. Does this loop set all four values for grade array or is it replacing the value of grade[0] everytime is executes?

     
     
    	System.out.println("Average");
     
     
    	for(int i = 0; i < average.length; i++)
    		{
    			average[i] = (testOne[i] + testTwo[i]) / 2;
     
     
     
    			if (average[i] >= 90 && average[i] <=100)
    				{
    				grade[i] = 'A';
    				}
    			else (average[i] >= 89 && average[i] <=80)
    				{
    				grade[i] = 'B';
    				}
    			else (average[i] >= 79 && average[i] <=70)
    				{
    				grade[i] = 'C';
    				}
    			else (average[i] < 70)
    				{
    				grade[i] = 'F';
    				}
    		}
    		} 
    	System.out.println("Grades");
    	System.out.println(grade[i]);
    }
    Last edited by Hazmat210; April 5th, 2012 at 04:02 PM.

  11. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Arraylist of averages of two arrays list

    Quote Originally Posted by Hazmat210 View Post
    So this below would be enough ? I just thought that a new loop would be necessary in order to set all four values in the grade array. Does this loop set all four values for grade array or is it replacing the value of grade[0] everytime is executes?

     
     
    	System.out.println("Average");
     
     
    	for(int i = 0; i < average.length; i++)
    		{
    			average[i] = (testOne[i] + testTwo[i]) / 2;
     
     
     
    			if (average[i] >= 90 && average[i] <=100)
    				{
    				grade[i] = 'A';
    				}
    			else (average[i] >= 89 && average[i] <=80)
    				{
    				grade[i] = 'B';
    				}
    			else (average[i] >= 79 && average[i] <=70)
    				{
    				grade[i] = 'C';
    				}
    			else (average[i] < 70)
    				{
    				grade[i] = 'F';
    				}
    		}
    		} 
    	System.out.println("Grades");
    	System.out.println(grade[i]);
    }
    It will set all four values of your grade array if you fix the errors. Have you try to compile it? I think it won't. You need to use an if.. else if... structure as i told you and post #4 describes.
    if (<condition 1>) {
    /* Java statements (1) */
    }
    else if (<condition 2>) {
    /* Java statements (2) */
    }
    else if (<condition n>) {
    /* Java statments (n) */
    }
    [<else clause>]

  12. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Arraylist of averages of two arrays list

    It will set all four values of your grade array if you fix the errors.
    I agree. Don't be afraid of trying something and posting back with the new code (all of it) and the compiler messages if those messages are not clear. Someone is sure to be able to explain what they mean.

    -----

    printf() can be considered a second matter and you can safely leave it until you have the arrays all containing the values they should.

  13. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Hazmat210 (April 5th, 2012)

  14. #10
    Junior Member Hazmat210's Avatar
    Join Date
    Feb 2012
    Location
    San Antonio,Tx
    Posts
    23
    My Mood
    Nerdy
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Arraylist of averages of two arrays list

    Ok so I got it all to work except for the formatting i've got some reading to do on formatting but input on how to make all nice and would be nice below is code so far everthing works as it should.
     
    import java.util.*;
     
    public class TestArray
    {
        public static void main (String[] args)
        {
     
    int[] testOne = new int[4];
    int[] testTwo = new int[4];
    double[] average = new double[4];
    char[] grade = new char[4];
     
    Scanner scan = new Scanner(System.in);
     
     
    // Input scores for both test and all four students.
     
            System.out.println("For test 1,");
     
            for ( int i = 0; i < testOne.length; i++)
                {
                    System.out.print("Enter score " + (i + 1) + ":");
                    testOne[i] = scan.nextInt();
                }
     
            System.out.println("For test 2,");
     
            for ( int i = 0; i < testTwo.length; i++)
                {
                    System.out.print("Enter score " + (i + 1) + ":");
                    testTwo[i] = scan.nextInt();
                }
     
            System.out.println("");
            System.out.println("Test 1");
     
            for(int i = 0; i < testOne.length; i++)
                {
                    System.out.println(testOne[i]);
                }
     
            System.out.println("");
            System.out.println("Test 2");
     
            for(int i = 0; i < testTwo.length; i++)
                {
                    System.out.println(testOne[i]);
                }
     
            System.out.println("Average");
     
     
            for(int i = 0; i < average.length; i++)
                {
                    average[i] = (testOne[i] + testTwo[i]) / 2;
     
     
     
                    if (average[i] >= 90 && average[i] <= 100)
                        {
                        grade[i] = 'A';
                        }
                    else if (average[i] >= 80 && average[i] <= 89)
                        {
                        grade[i] = 'B';
                        }
                    else if (average[i] >= 70 && average[i] <= 79)
                        {
                        grade[i] = 'C';
                        }
                    else if(average[i] < 70)
                        {
                        grade[i] = 'F';
                        }
     
                    System.out.println(average[i]);
                 }
     
                System.out.println("Grades");
     
                for(int i = 0; i < grade.length; i++)
                    {
                    System.out.println(grade[i]);
                    }
     
                    }
    }
    Last edited by Hazmat210; April 5th, 2012 at 11:40 PM. Reason: problem partially solved.

  15. #11
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Arraylist of averages of two arrays list

    Quote Originally Posted by Hazmat210 View Post
    ok tried this but i'm getting:
    C:\Users\Gonzalez\Desktop\Java Chapter 5\TestArray.java:81: reached end of file while parsing
    }
    1 error
    Tool completed with exit code 1
    You have probably forgotten to close a curly brace.

  16. #12
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Arraylist of averages of two arrays list

    reached end of file while parsing
    This message often indicates that you have missed a closing } brace somewhere.

    Correct - make consistent - your indentation so that every } lines up with its corresponding opening { and you should be able to spot what's missing.

Similar Threads

  1. Arrays and array list
    By rob17 in forum Collections and Generics
    Replies: 2
    Last Post: February 19th, 2012, 06:10 PM
  2. arraylist, hashtable, and 1~2 dimension arrays
    By Perd1t1on in forum Object Oriented Programming
    Replies: 4
    Last Post: July 25th, 2010, 01:25 PM
  3. Need help outputting sub-list of an ArrayList
    By Allusive in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2010, 09:20 AM
  4. Averages, Highest & using a .csv file ! ? ! ?
    By thebigtimeGNAR in forum Java Theory & Questions
    Replies: 3
    Last Post: April 14th, 2010, 11:35 AM
  5. Merging Arrays Vs. Linked List.
    By Kumarrrr in forum Collections and Generics
    Replies: 1
    Last Post: March 1st, 2010, 03:20 AM

Tags for this Thread