# Algorithm problem

• 07-01-2007, 01:29 AM
Marcus
Algorithm problem
This code is supposed to allow the user to enter a 5 student IDs and their results and calculate the total and show the grades all the information should be shown in a table. For some reason the first studentID is indented - does anyone know why?

Code:

```import java.util.*;  public class Assignment  {         public static void main(String[] args)         {         //Declares variables and arrays         int i = 0;         int j = 0;         String [] studentIDArray = new String [5];         int [][] studentArray = new int [5][9];         Scanner kybd = new Scanner(System.in);           //Welcome message to user         System.out.println("Welcome");         System.out.println("~~~~~~~");         System.out.println("Please note any unanswered questions should marked as -1");         System.out.print ("\n");           //User is prompted to enter student ID and the results         for (i = 0; i < 5; ++i)                 {                 System.out.print("Please Enter the Student ID: ");                 studentIDArray[i] = kybd.next();                 for (j = 0; j < 8; ++j)                         {                         System.out.println("Please Enter the Result for Question " + (j+1));                         studentArray[i][j] = kybd.nextInt();                         }                 }           //Calculates the students total         while (j < 8)                 {                 j = 0;                 while (i < 5)                         {                         studentArray[i][9] += studentArray[i][j];                         j+=1;                         }                 i+=1;                 }           int k = 0;         String grade = "";         String [] gradeArray = new String [5];        //Declares new array to store grades           //Calculates the grades for each student         while (i < 5)                 {                 studentArray[i][8] += 3;                 if (studentArray[i][8] > 70)                         {                         gradeArray[k] = "Distinction";                         }                               else if (studentArray[i][8] > 60)                         {                                 gradeArray[k] = "Merit";                             }                               else if (studentArray[i][8] > 40)                         {                         gradeArray[k] = "Pass";                         }                         else if (studentArray[i][8] >= 40)                         {                         gradeArray[k] = "Fail";                         }                 k+=1;                 i+=1;                 }           System.out.print ("\n");         System.out.println ("StudentID Q1  Q2    Q3    Q4    Q5    Q6    Q7    Q8    Total  Grade");         System.out.print ("  ");           i=0;         j=0;         k=0;           //Output students results         while (i < 5)         {                 j = 0;                 System.out.printf(studentIDArray[i]);                 while (j < 9)                 {                         System.out.printf ("%7d", studentArray[i][j]);                         j+=1;                 }                   System.out.print("    " + gradeArray[k]);                 System.out.print ("\n");                 i+=1;                 k+=1;         }  }  }```
Marcus:cool:
• 07-01-2007, 01:33 AM
Felissa
Well, one thing at a time:

Code:

```System.out.println ("StudentID Q1  Q2    Q3    Q4    Q5    Q6    Q7    Q8    Total  Grade");  System.out.print ("  ");```
Why are you printing two spaces - without a newline after the header? It will simply add these two spaces to the next line - like it does.

As far as the other issues - I'd strongly encourage you to add some System.out.prinln() statements here and there to understand what is going on.

Felissa:p
• 07-01-2007, 01:37 AM
Eric
Can I make a suggestion? - print out the value of "i" and "j" before you enter the while loops. It looks like you're incrementing but never resetting them.

Eric