Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: issues with 2d array and creating methods using user input

  1. #1
    glisson.ar is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default issues with 2d array and creating methods using user input

    I was given the top part of my code. I am supposed to write 3 methods that use a 2d array from user input and then print out the results. However, I am not able to get the user input to work correctly and I'm not sure what I am doing wrong. I am attaching the code (unfortunately I had to zip the file to upload it) and this is what the printed output should resemble:

    SAMPLE OUTPUT 1:

    5 student(s) took 2 test(s).

    Enter student 1 name: Fred
    Enter student 2 name: Sue
    Enter student 3 name: Ed
    Enter student 4 name: Mary
    Enter student 5 name: Tom

    Entering scores for Fred:
    Enter score for test #1: 78
    Enter score for test #2: 80

    Entering scores for Sue:
    Enter score for test #1: 91
    Enter score for test #2: 94

    Entering scores for Ed:
    Enter score for test #1: 65
    Enter score for test #2: 90

    Entering scores for Mary:
    Enter score for test #1: 100
    Enter score for test #2: 98

    Entering scores for Tom:
    Enter score for test #1: 93
    Enter score for test #2: 87

    STUDENT REPORT
    Scores for Fred:
    Test #1: 78
    Test #2: 80
    Average for Fred: 79.0
    Scores for Sue:
    Test #1: 91
    Test #2: 94
    Average for Sue: 92.5
    Scores for Ed:
    Test #1: 65
    Test #2: 90
    Average for Ed: 77.5
    Scores for Mary:
    Test #1: 100
    Test #2: 98
    Average for Mary: 99.0
    Scores for Tom:
    Test #1: 93
    Test #2: 87
    Average for Tom: 90.0

    TEST REPORT
    Scores for test1:
    Fred: 78
    Sue: 91
    Ed: 65
    Mary: 100
    Tom: 93
    Test #1 average: 85.4
    Scores for test2:
    Fred: 80
    Sue: 94
    Ed: 90
    Mary: 98
    Tom: 87
    Test #2 average: 89.8

    SAMPLE OUTPUT 2:

    2 student(s) took 4 test(s).

    Enter student 1 name: Jane
    Enter student 2 name: John

    Entering scores for Jane:
    Enter score for test #1: 95
    Enter score for test #2: 92
    Enter score for test #3: 90
    Enter score for test #4: 100

    Entering scores for John:
    Enter score for test #1: 89
    Enter score for test #2: 90
    Enter score for test #3: 86
    Enter score for test #4: 97

    STUDENT REPORT
    Scores for Jane:
    Test #1: 95
    Test #2: 92
    Test #3: 90
    Test #4: 100
    Average for Jane: 94.25
    Scores for John:
    Test #1: 89
    Test #2: 90
    Test #3: 86
    Test #4: 97
    Average for John: 90.5

    TEST REPORT
    Scores for test1:
    Jane: 95
    John: 89
    Test #1 average: 92.0
    Scores for test2:
    Jane: 92
    John: 90
    Test #2 average: 91.0
    Scores for test3:
    Jane: 90
    John: 86
    Test #3 average: 88.0
    Scores for test4:
    Jane: 100
    John: 97
    Test #4 average: 98.5
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: issues with 2d array and creating methods using user input

    You should be able to copy and paste your code into the thread as text. Be sure to wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    glisson.ar is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: issues with 2d array and creating methods using user input

    Quote Originally Posted by Norm View Post
    You should be able to copy and paste your code into the thread as text. Be sure to wrap the code in code tags.
    Thank you

  4. #4
    glisson.ar is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: issues with 2d array and creating methods using user input

    The is the program that I attached.
    Java Code:
    import static java.lang.System.out;
    import java.util.*;
    
    public class StuTest
    {
       private static Scanner keyboard = new Scanner(System.in);
    
       public static void main(String[] args)
       {
          int[][] testScores; // [students][tests]
          String[] stuNames;
    
          testScores = createArray();
          out.println(testScores.length + " student(s) took " +
             testScores[0].length + " test(s).\n");
    
          stuNames = new String[testScores.length];
          populateNames(stuNames);
    
          populateTestScores(stuNames,testScores);
    
          printStudentReport(stuNames,testScores);
         // printTestReport(stuNames,testScores);
       }
    
       public static int[][] createArray()
       {
          Random rand = new Random();
          int students = rand.nextInt(5) + 1;
          int tests = rand.nextInt(4) + 1;
          return new int[students][tests];
       }
    
       public static void populateNames(String[] names)
       {
          for(int i = 0; i<names.length; i++)
          {
             out.print("Enter student " + (i+1) + " name: ");
             names[i] = keyboard.next();
          }
          out.println();
       }
    
       // Code your 3 methods below...
    
    	public static void populateTestScores(String[] names, int[][] scores)
    	{
    		for (int i=0; i<names.length; i++)
    		{
    			out.println("Entering scores for "+ names[i]);
    
    			for(int students=1; students<scores.length; students++)
    			{
    				out.printf("     Enter score for test # %d:  ", students);
    
    				for (int tests=0; tests<scores[students].length; tests++)
    				{
    
    					scores[students][tests] = keyboard.nextInt();
    				}
    				out.println();
    			}
    			out.println();
    		}
    	}
    	public static void printStudentReport(String[] names, int[][] scores)
    	{
    		out.println("Student Report");
    		int sum= 0;
    		double avg;
    		for (int i=0; i<=names.length; i++)
    		{
    			out.println("Scores for "+ names[i]);
    			for (int students=1; students<=scores.length; students++)
    			{
    				for (int tests=0; tests<=scores[students].length; tests++)
    				{
    					out.println("Test "+(students)+ ":  "+ (scores[tests]));
    					sum= sum + scores[students][tests];
    
    				}
    			}
    			out.println(sum);
    			//avg= double sum/scores[students].length;
    			//out.printf("Average for %s "+ avg, names[students] );
    		}
    	}
    	public static void printTestReport(String[] names, int[][] scores)
    	{
    		out.println("Test Report");
    
    
    	}
    
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: issues with 2d array and creating methods using user input

    not able to get the user input to work correctly
    Please explain what the program does now and what you want to change about the way it works.
    Copy the full contents of the command prompt window from when you test it and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    glisson.ar is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: issues with 2d array and creating methods using user input

    Quote Originally Posted by Norm View Post
    Please explain what the program does now and what you want to change about the way it works.
    Copy the full contents of the command prompt window from when you test it and paste it here.
    The program is supposed to take user input for test grades. However I can not get that to work correctly. It is also supposed to show the average test grades for each student.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: issues with 2d array and creating methods using user input

    I can not get that to work correctly.
    Please explain.
    What happens when you compile and execute the program?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: issues with 2d array and creating methods using user input

    compilation wise ok..but lot lot of logical issues..random and all why u want to use?
    in ur program u want to keyin studentsname and marks..finally u are trying to display name with total.I feel lot of unwanted complications in ur logic
    Ramya:cool:

  9. #9
    glisson.ar is offline Member
    Join Date
    Sep 2015
    Posts
    5
    Rep Power
    0

    Default Re: issues with 2d array and creating methods using user input

    Quote Originally Posted by RamyaSivakanth View Post
    compilation wise ok..but lot lot of logical issues..random and all why u want to use?
    in ur program u want to keyin studentsname and marks..finally u are trying to display name with total.I feel lot of unwanted complications in ur logic
    The only part of the program that belongs to me starts at line 44. I was given the information prior to that. While I would not necessarily write a program in this fashion, I do not have a choice. I have to create 3 methods and within those methods use a string array for the names and a 2d array for the test number and test scores.. The first method is to have a user enter the test scores, second method is to print a student report with the student name, test scores and average test score, and the third method is to print a test report with scores for each student for each test with an average for each test.. I am gave an example of what the output is supposed to look like in the beginning.
    Last edited by glisson.ar; 09-21-2015 at 01:07 AM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: issues with 2d array and creating methods using user input

    Can you make changes to any of the code while you are making changes to make it easier to debug?
    For example
    hard code the array sizes vs using random values. For example:
    Java Code:
       int students = 3; //rand.nextInt(5) + 1;  // for testing
    Hard code the user's input by using the Scanner class's constructor to use a String with all the input predefined:
    Java Code:
      Scanner scnr = new Scanner("The input to be used would go here");
    That would make the testing much easier.

    When the initial testing was done, the changes would be removed.

    Can you answer these questions I asked?
    Please explain.
    What happens when you compile and execute the program?
    Last edited by Norm; 09-21-2015 at 01:19 AM.
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: issues with 2d array and creating methods using user input

    Great idea to use hard coded values. The OP might make it easier by just creating new populateXXXXDebug methods to programmatically populate the appropriate arrays. Then just remove them later after testing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Creating 2D array from all user input
    By peek_a_boo in forum New To Java
    Replies: 1
    Last Post: 12-08-2011, 08:16 PM
  2. Creating a number of objects based on user input.
    By Kevinius in forum New To Java
    Replies: 21
    Last Post: 04-03-2011, 08:53 AM
  3. Creating a Table with user input
    By JonniBravo in forum Eclipse
    Replies: 1
    Last Post: 09-08-2010, 12:50 PM
  4. Scanner Issues (User Input: Very Simple)
    By carlodelmundo in forum New To Java
    Replies: 8
    Last Post: 10-31-2008, 02:44 AM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 AM

Tags for this Thread

Posting Permissions

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