943,962 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1933
  • Java RSS
Feb 6th, 2006
0

Program compiles but has runtime problems

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yuribokovich221 is offline Offline
3 posts
since Feb 2006
Feb 6th, 2006
0

Re: Program compiles but has runtime problems

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 6th, 2006
0

Re: Program compiles but has runtime problems

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yuribokovich221 is offline Offline
3 posts
since Feb 2006
Feb 7th, 2006
0

Re: Program compiles but has runtime problems

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?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 7th, 2006
0

Re: Program compiles but has runtime problems

Quote 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?
Quote 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.
.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Yuribokovich221 is offline Offline
3 posts
since Feb 2006
Feb 7th, 2006
0

Re: Program compiles but has runtime problems

You don't say what happens, nor what's supposed to happen.
Then you say you can't be bothered to be more specific.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Stack- need help
Next Thread in Java Forum Timeline: need some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC