Program compiles but has runtime problems

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2006
Posts: 3
Reputation: Yuribokovich221 is an unknown quantity at this point 
Solved Threads: 0
Yuribokovich221 Yuribokovich221 is offline Offline
Newbie Poster

Program compiles but has runtime problems

 
0
  #1
Feb 6th, 2006
I would not be posting right now if it weren't for the fact that I've exausted my resources and have no where else to turn for a solution.

First of all, let me give you guys a .class file, that when compiled, does exactly what my program is -supposed- to do.

finished program

Now that you know what my program is supposed to do, here is the program I have:

  1. import static java.lang.System.out;
  2. import java.util.Scanner;
  3.  
  4. public class StuTest
  5. {
  6. private static Scanner keyboard = new Scanner(System.in);
  7.  
  8. public static void main(String[] args)
  9. {
  10. int[][] testScores; // [students][tests]
  11. String[] stuNames;
  12.  
  13. testScores = createArray();
  14. if(testScores != null)
  15. {
  16. stuNames = new String[testScores.length];
  17. populateNames(stuNames);
  18. populateTestScores(stuNames,testScores);
  19. printStudentReport(stuNames,testScores);
  20. printTestReport(stuNames,testScores);
  21. out.println("\n\nGoodbye!\n\n");
  22. }
  23. else
  24. {
  25. out.println("Array not successfully created - exiting");
  26. }
  27. }
  28.  
  29. // Code your 5 methods below..
  30. // Ask user to enter the number of students, and then asks user to enter the number of tests.
  31. private static int numberOfStudents;
  32. private static int numberOfTests;
  33. public static int[][] createArray()
  34. {
  35. System.out.print("Enter the number of students: ");
  36. numberOfStudents = keyboard.nextInt();
  37. System.out.print("Enter the number of tests: ");
  38. numberOfTests = keyboard.nextInt();
  39. System.out.println();
  40. System.out.println();
  41. int[][] stuArray = new int[numberOfStudents][numberOfTests];
  42. return stuArray;
  43. }
  44. // Ask for the name of each student for each number of students
  45. public static String[] populateNames(String[] stuNames)
  46. {
  47. for(int x = 0; x < stuNames.length; x++)
  48. {
  49. System.out.print("Enter the name for student " + (x + 1) + ": ");
  50. keyboard = new Scanner(System.in);
  51. stuNames[x] = keyboard.nextLine();
  52. }
  53. return stuNames;
  54. }
  55. // Display "Entering Scores for (Student Name)" then input the score for each test.
  56. public static int[][] populateTestScores(String[] stuNames, int[][] testScores)
  57. {
  58. for(int y = 0; y < stuNames.length; y++)
  59. {
  60. for(int u = 0; u < stuNames.length; u++)
  61. {
  62. System.out.println("Entering scores for " + stuNames[y]);
  63. System.out.print(" Enter score for Test " + testScores + ": ");
  64. testScores[y][u] = keyboard.nextInt();
  65. }
  66. }
  67. return testScores;
  68. }
  69. // Print Student Report
  70. public static void printStudentReport(String[] stuNames, int[][] testScores)
  71. {
  72. // Print the name of each student and the scores for each of the student's tests
  73. System.out.println("STUDENT REPORT");
  74. for(int z = 1; z <= stuNames.length; z++)
  75. {
  76. System.out.println("Report for " + stuNames[z]);
  77. for(int a = 1; a <= numberOfTests; a++)
  78. {
  79. System.out.println(" Test " + a + ": " + testScores[a][z]);
  80. }
  81. // Print the average of the student's tests
  82. int p = 0;
  83. for(int o = 1; o <= testScores.length; o++)
  84. {
  85.  
  86. p += testScores[o][z];
  87. }
  88. p /= testScores.length;
  89. System.out.println(" " + stuNames[z] + "'s average is " + p);
  90. }
  91. // Print Test Report
  92. }
  93. public static void printTestReport(String[] stuNames, int[][] testScores)
  94. {
  95. System.out.println("TEST REPORT");
  96. // Print the results for each individual test, and the average for each test
  97. for(int b = 1; b <= testScores.length; b++)
  98. {
  99. System.out.println("Report for Test" + b);
  100. for(int c = 1; c <= numberOfStudents; c++)
  101. {
  102. System.out.println(" Test " + c + ": " + testScores[c][b]);
  103. }
  104. int p = 0;
  105. for(int o = 1; o <= testScores.length; o++)
  106. {
  107. p += testScores[o][b];
  108. }
  109. p /= testScores.length;
  110. System.out.println(" Test" + b + " average is " + p);
  111. }
  112.  
  113. }
  114. }

If you throw that into your compiler, it will compile, but gives several runtime errors resulting in a crash.. What happens is that when I get to the "Entering scores for test", the test numbers do not show up correctly. It prints out the student report inproperly and does not print out the test report at all, it just crashes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Program compiles but has runtime problems

 
0
  #2
Feb 6th, 2006
What's the error message you are recieving(if any)?

Also, Im too lazy to put that into a compiler and read through all the source, so please explain what it's doing that it's not suppose to in detail.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 3
Reputation: Yuribokovich221 is an unknown quantity at this point 
Solved Threads: 0
Yuribokovich221 Yuribokovich221 is offline Offline
Newbie Poster

Re: Program compiles but has runtime problems

 
0
  #3
Feb 6th, 2006
Well it might take awhile to explain, the best and easiest thing for you and me would be to take a look at the .class file I have and then compile the code I have and compare the two. But I will try to explain... What it does is ask for the number of students, and then the number of tests. Then it should ask for the names of each student. Then it asks for the test scores for each student. After that, it gives a student report which has the scores and average for each individual, and then a test report that has the scores and average for each test.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Program compiles but has runtime problems

 
0
  #4
Feb 7th, 2006
no, the best way is for you to think about your problems yourself.
If you're too lazy to even try to explain what is going wrong why should anyone go to the effort (especially unpaid) to analyse your problems for you?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 3
Reputation: Yuribokovich221 is an unknown quantity at this point 
Solved Threads: 0
Yuribokovich221 Yuribokovich221 is offline Offline
Newbie Poster

Re: Program compiles but has runtime problems

 
0
  #5
Feb 7th, 2006
Originally Posted by jwenting
no, the best way is for you to think about your problems yourself.
If you're too lazy to even try to explain what is going wrong why should anyone go to the effort (especially unpaid) to analyse your problems for you?
Originally Posted by yuribokovich221
If you throw that into your compiler, it will compile, but gives several runtime errors resulting in a crash.. What happens is that when I get to the "Entering scores for test", the test numbers do not show up correctly. It prints out the student report inproperly and does not print out the test report at all, it just crashes.
.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Program compiles but has runtime problems

 
0
  #6
Feb 7th, 2006
You don't say what happens, nor what's supposed to happen.
Then you say you can't be bothered to be more specific.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC